From 9cffe158a70f4945a6bf000dace4eddf4ca2c344 Mon Sep 17 00:00:00 2001 From: James Halliday Date: Thu, 26 Aug 2010 19:53:16 -0800 Subject: [PATCH] memoization for keys, values, length --- lib/hash.js | 37 ++++++++++++++++++++++--------------- package.json | 2 +- test/hash.js | 8 ++++++++ 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/lib/hash.js b/lib/hash.js index cf3c06d..a55c478 100644 --- a/lib/hash.js +++ b/lib/hash.js @@ -14,6 +14,7 @@ function Hash (hash, extra) { Object.keys(hash).forEach(function (key) { f.call(hash, hash[key], key); }); + memoized = {}; return self; }, filter : function (f) { @@ -37,31 +38,34 @@ function Hash (hash, extra) { Object.keys(h).forEach(function (key) { hash[key] = h[key]; }); + memoized = {}; return self; }, merge : function (h) { - var acc = {}; - Object.keys(hash).forEach(function (key) { - acc[key] = hash[key]; - }); - Object.keys(h).forEach(function (key) { - acc[key] = h[key]; - }); - hash = acc; - return self; + return self.clone.update(h); + }, + tap : function (f) { + f.call(self, hash); + memoized = {}; }, - tap : function (f) { f.call(self, hash) }, end : hash, items : hash }; + var memoized = {}; Object.defineProperty(self, 'keys', { get : function () { - return Object.keys(hash); + if (memoized.keys === undefined) { + memoized.keys = Object.keys(hash); + } + return memoized.keys } }); Object.defineProperty(self, 'values', { get : function () { - return Object.keys(hash) - .map(function (key) { return hash[key] }) + if (memoized.values === undefined) { + memoized.values = Object.keys(hash) + .map(function (key) { return hash[key] }); + } + return memoized.values; } }); Object.defineProperty(self, 'clone', { get : function () { @@ -73,7 +77,10 @@ function Hash (hash, extra) { } }); Object.defineProperty(self, 'length', { get : function () { - return Object.keys(hash).length; + if (memoized.length === undefined) { + memoized.length = Object.keys(hash).length; + } + return memoized.length; } }); return self; @@ -108,6 +115,6 @@ Hash.reduce = function (ref, f, acc) { Hash.concat = function (xs) { var hash = Hash({}); xs.forEach(function (x) { hash.update(x) }); - return hash; + return hash.items; }; diff --git a/package.json b/package.json index faa42c3..393d5fc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name" : "traverse", - "version" : "0.0.7", + "version" : "0.0.8", "description" : "Traverse and transform objects by visiting every node on a recursive walk.", "author" : "James Halliday", "modules" : { diff --git a/test/hash.js b/test/hash.js index f59d739..02b57cf 100755 --- a/test/hash.js +++ b/test/hash.js @@ -57,5 +57,13 @@ exports['hash traversal'] = function (assert) { assert.equal(ref1.a, 1); assert.equal(ref1.b, 2); + var merged = hash3.merge({ c : 3.14, d : 4 }); + assert.equal(merged.items.a, 0); + assert.equal(merged.items.b, 2); + assert.equal(merged.items.c, 3.14); + assert.equal(merged.items.d, 4); + assert.equal(merged.length, 4); + assert.equal(hash3.items.c, 3); + assert.equal(hash3.length, 3); };