From 07c08e076296497eb64ae50abcb2c9618632b827 Mon Sep 17 00:00:00 2001 From: Martin Eisenhardt Date: Thu, 15 May 2008 16:28:52 +0200 Subject: [PATCH] Pre-compute collection for us_zones convenience method. The us_zones convenience method in TimeZone generates the list of TimeZone objects in the U.S. every time it is called. This is inefficient, especially in applications that use time zones very heavily. This commit changes us_zones so that it pre-computes the collection; it generates the collection once when the class is loaded, and stores the collection in a constant. Subsequent invocations use the cached collection and are much faster. Signed-off-by: Martin Eisenhardt --- .../lib/active_support/values/time_zone.rb | 16 +++++++++++----- 1 files changed, 11 insertions(+), 5 deletions(-) diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index 0fa9913..b3c2e9b 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -327,6 +327,16 @@ class TimeZone ZONES_MAP.freeze end + # A regular expression that matches the names of all time zones in + # the USA. + US_ZONES_REGEX = /US|Arizona|Indiana|Hawaii|Alaska/.freeze + + unless const_defined?(:US_ZONES) + US_ZONES = ZONES.find_all { |z| z.name =~ US_ZONES_REGEX } + US_ZONES.sort! + US_ZONES.freeze + end + class << self alias_method :create, :new @@ -361,14 +371,10 @@ class TimeZone end end - # A regular expression that matches the names of all time zones in - # the USA. - US_ZONES = /US|Arizona|Indiana|Hawaii|Alaska/.freeze - # A convenience method for returning a collection of TimeZone objects # for time zones in the USA. def us_zones - all.find_all { |z| z.name =~ US_ZONES } + US_ZONES end end end -- 1.5.2.5