Skip to content

Instantly share code, notes, and snippets.

@thomasmichaelwallace
Created July 28, 2017 17:15
Show Gist options
  • Save thomasmichaelwallace/cbfcea66598247ae0dccfc50edd2bf45 to your computer and use it in GitHub Desktop.
Save thomasmichaelwallace/cbfcea66598247ae0dccfc50edd2bf45 to your computer and use it in GitHub Desktop.
A quick and dirty look at the overhead of calling a double-dot method.
const testCount = 1e7;
const keyCount = 10;
const times = count => Array.from(Array(count))
const message = (test, runtime) => `${test} took ${((runtime[0] * 1e9) + runtime[1]) / 1e6}ms`;
const obj = times(keyCount)
.map(Math.random)
.reduce((o, p) => Object.assign({}, o, { [p.toString()]: Math.random() }), {});
const hasKey = Object.keys(obj)[0];
const hasNotKey = 'missing';
const cached = process.hrtime();
const has = Object.prototype.hasOwnProperty;
times(testCount).map(() => {
const one = has.call(obj, hasKey) === true;
const two = has.call(obj, hasNotKey) === true;
return one && two;
});
console.log(message('Cached', process.hrtime(cached)));
const nested = process.hrtime();
times(testCount).map(() => {
const one = Object.prototype.hasOwnProperty.call(obj, hasKey) === true;
const two = Object.prototype.hasOwnProperty.call(obj, hasNotKey) === true;
return one && two;
});
console.log(message('Nested', process.hrtime(nested)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment