Skip to content

Instantly share code, notes, and snippets.

@jordwest
Last active January 27, 2017 08:10
Show Gist options
  • Save jordwest/8c884e57b2f056911ed68710d60e4b8a to your computer and use it in GitHub Desktop.
Save jordwest/8c884e57b2f056911ed68710d60e4b8a to your computer and use it in GitHub Desktop.
function benchmark( times, fn ) {
var start = performance.now()
for( var i = 0; i < times; i++ ) {
fn();
}
return ( performance.now() - start ) / times;
}
var plainObject = {
TEST: 'TEST',
TEST2: 'TEST2',
}
var withProto = Object.create( null );
withProto.TEST = 'TEST';
withProto.TEST2 = 'TEST2';
var x, y, a, b;
// Ignore these results to ensure a warm start later
benchmark( 10000000, function() {
b = plainObject.TEST;
} );
benchmark( 10000000, function() {
a = withProto.TEST;
} );
var plainObjectTime = benchmark( 1000000000, function() {
y = plainObject.TEST;
} );
var withProtoTime = benchmark( 1000000000, function() {
x = withProto.TEST;
} );
console.log( 'plain object', plainObjectTime, 'ms' );
console.log( 'with prototype ', withProtoTime, 'ms' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment