Skip to content

Instantly share code, notes, and snippets.

@gordonbrander
Created December 17, 2013 17:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gordonbrander/8009331 to your computer and use it in GitHub Desktop.
Save gordonbrander/8009331 to your computer and use it in GitHub Desktop.
Enumerate object's own keys
// Enumerate over an object's own keys/values, accumulating a value.
// `initial` defines the initial value for the accumulation. Reference is an
// optional additional argument that make a common case -- comparing 2
// objects -- easy and more efficient.
function enumerate(object, next, initial, reference) {
var accumulated = initial;
for (var key in object)
// Test for own keys with `hasOwnProperty` instead of `Object.keys` to
// avoid garbage creation.
//
// @TODO need to test this "optimization".
if(object.hasOwnProperty(key)) accumulated = next(accumulated, key, object[key], object, reference);
return accumulated;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment