delete v. _.omit

JavaScript performance comparison

Revision 2 of this test case created by tomByrer

Info

In my Backbone application I'm receiving data from the server and modify it in a model's parse method as the return value of the parse method is what the Backbone Model will turn into the attributes. While almost all of the keys should become attributes, there is one that should not.

Previously I was deleteing that value from the response object passed to the parse method. I've been told that delete in JavaScript is an expensive operation. The other options would be to use underscore's omit method which I know creates a new object and copies keys and values over. I decided to find out which is more performant.

v2 ~tomByrer: + lodash

Preparation code

<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script>
<script>
var us = _.noConflict();
</script>
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
<script>
var ld = _.noConflict();
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
delete
var obj = {
  key: 'value'
};
delete obj.key;
return obj;
pending…
Underscore.omit
var obj = {
  key: 'value'
};
return us.omit(obj, 'key');
pending…
Lo-Dash.omit
var obj = {
  key: 'value'
};
return ld.omit(obj, 'key');
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

1 comment

tomByrer (revision owner) commented :

Lo-Dash is much faster than Underscore in Chrome, but a bit slower in other browsers. I assume Chrome/V8 uses the callback in Lo-Dash (good news for Node users). UserAgent  Lo-Dash.omit  Underscore.omit  delete  #Tests  Chrome 31.0.1650 2,062,725 368,492 2,284,877 2 IE 10.0 400,423 668,851 2,450,019 2 Pale Moon (Firefox Variant) 24.2.2 196,101 235,086 1,907,312 2 https://github.com/lodash/lodash/blob/6ba2730c3fdf33db6f675d222194739b3c39aa22/dist/lodash.compat.js#L3074 https://github.com/jashkenas/underscore/blob/8bb4c9e25a72415918d701f8a43489626e7fc891/underscore.js#L853

Add a comment