jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
test what is the fastest for getters inside objects. of course closures are best OOP-wise, but are they also the fastest? I test 4 examples: use of closure function use of window.data use of this.data use of jQuery $.data
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
var myObject1 = (function() {
var foo = 42;
return {
getFoo: function() {
return foo;
},
setFoo: function(val) {
foo = val;
}
};
}());
window.foo = 42;
var myObject2 = {
'getFoo': function() {
return window.foo;
},
'setFoo': function(val) {
window.foo = val;
}
};
var myObject3 = {
'foo': 42,
'getFoo': function() {
return this.foo;
},
'setFoo': function(val) {
this.foo = val;
}
};
jQuery.data(document.body, 'foo', 42);
var myObject4 = {
'getFoo': function() {
return jQuery.data(document.body, 'foo');
},
'setFoo': function(val) {
jQuery.data(document.body, 'foo', val);
}
};
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
save in closure |
| ready |
save in window |
| ready |
save in object |
| ready |
use jQuery.data |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.