Skip to content

Commit

Permalink
memoization for keys, values, length
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Aug 27, 2010
1 parent 7fc4eca commit 9cffe15
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
37 changes: 22 additions & 15 deletions lib/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 () {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
};

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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" : {
Expand Down
8 changes: 8 additions & 0 deletions test/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

0 comments on commit 9cffe15

Please sign in to comment.