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
Somebody asked for a benchmark here: http://webreflection.blogspot.com/2011/08/please-stop-reassigning-for-no-reason.html
However, the whole point is not about raw performances but implications that common patter to assign a potentially not defined property cause.
As example, consider it is not even possible to compare an object with a getter only since the common pattern will simply throw an Error
during assignment.
obj.prop = // if no setter: ERROR
obj.prop // getter
|| {}
;
With "prop" in obj
everything would be still OK.
<script>
// all functions should avoid JIT optimizations
// (e.g. if nothing happens JIT may nullify function body)
// hopefully assigning the object to a non GC global variable
// will do the trick ... let's see ...
function inOperator(object) {
"prop" in object || (object.prop = {});
return object;
}
function commonPattern(object) {
object.prop = object.prop || {};
return object;
}
function betterPattern(object) {
object.prop || (object.prop = {});
return object;
}
var
// simple shim that *should* fail in old browsers
// if getters/setters are not supported
defineProperty = Object.defineProperty || function (o, k, v) {
o.__defineGetter__(k, v.get);
o.__defineSetter__(k, v.set);
return o;
},
globalResult
;
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
in operator set |
| ready |
in operator |
| ready |
in operator + magic set |
| ready |
in operator + magic |
| ready |
common pattern set |
| ready |
common pattern |
| ready |
common pattern + magic set |
| ready |
common pattern + magic |
| ready |
better pattern set |
| ready |
better pattern |
| ready |
better pattern + magic set |
| ready |
better pattern + magic |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.