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
The delete operator removes a property entirely. Setting a property to null or undefined just changes the value of the key.
Assinging null/undefined is not equivalent to delete since the property can still be enumerated via a for-in loop, Object.keys, Object.getOwnPropertyNames, etc. but in practice they are often used to mean the same thing: that a property is unset.
Using 'delete' is conceptually preferable but the big problem is how the JavaScript engine chooses to react to "delete". JS Engines use "hidden classes" (Shapes) to optimise the code but "delete" supposedly downgrades from a Shape to a hash table which makes every property access significantly slower. To test if this is what actually happens or not, we compare accessing an object's properties after setting to null/undefined vs accessing after deleting to see if the Shape is lost or not (performance for accessing properties got worse).
<script>
'use strict';
var template = {
a: 1,
b: 2,
c: 3,
d: 4,
e: 5,
f: 6,
g: 7,
h: 8,
i: 9,
j: '10',
k: 11,
l: '12',
m: function() {
return 13;
},
n: 14,
o: function() {
return '15';
}
};
var dump, log = function() {
dump = arguments;
};
</script>
var o = {};
for (var x in template) {
o[x] = template[x];
}
Ready to run.
Test | Ops/sec | |
---|---|---|
Baseline (no modification) |
| ready |
Setting properties to null |
| ready |
Setting properties to undefined |
| ready |
Deleting properties |
| ready |
Setting properties to undefined + enumerable set to false |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.