Created
November 10, 2018 18:00
-
-
Save nolanlawson/fc120595a764835fb3f6a1ba38752229 to your computer and use it in GitHub Desktop.
dot versus argument bracket versus bracket benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>dot versus bracket access</title> | |
</head> | |
<body> | |
<h1>dot versus bracket access</h1> | |
<pre id="display"></pre> | |
<script> | |
(function () { | |
'use strict' | |
var obj = { | |
foo: 'foo', | |
bar: 'bar', | |
baz: 'baz' | |
} | |
function callWithDot() { | |
return obj.foo | |
} | |
function callWithSquareBrackets(key) { | |
return obj[key] | |
} | |
function callWithSquareBracketsStringInline() { | |
return obj['foo'] | |
} | |
var foo | |
performance.mark('dot-start') | |
for (var i = 0; i < 1000000; i++) { | |
foo = callWithDot() | |
} | |
performance.mark('dot-end') | |
performance.measure('dot', 'dot-start', 'dot-end') | |
performance.mark('bracket-start') | |
for (var i = 0; i < 1000000; i++) { | |
foo = callWithSquareBrackets('foo') | |
} | |
performance.mark('bracket-end') | |
performance.measure('bracket', 'bracket-start', 'bracket-end') | |
performance.mark('bracket-string-start') | |
for (var i = 0; i < 1000000; i++) { | |
foo = callWithSquareBrackets('foo') | |
} | |
performance.mark('bracket-string-end') | |
performance.measure('bracket-string', 'bracket-string-start', 'bracket-string-end') | |
document.getElementById('display').innerHTML = '' + | |
'\nobj.foo : ' + performance.getEntriesByName('dot')[0].duration + | |
'\nobj[key] : ' + performance.getEntriesByName('bracket')[0].duration + | |
'\nobj["foo"]: ' + performance.getEntriesByName('bracket-string')[0].duration | |
})() | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment