delete v. _.omit

JavaScript performance comparison

Test case created by Aaron

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script>
    

Preparation code output

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
delete
var obj = {
  key: 'value'
};
delete obj.key;
return obj;
pending…
_.omit
var obj = {
  key: 'value'
};
return _.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.

4 Comments

Aaron (revision owner) commented :

Soooooo delete by a landslide :D

John-David Dalton commented :

Not really the same thing. delete mutates the actual object, while _.omit returns a new object.

Also since delete mutates the actual object, the property is removed on the very first iteration so the thousands after are not really doing much :P

Aaron (revision owner) commented :

I was able to confirm that jsperf doesn't create a new object for each iteration of the test. I modified it so that it has a fresh object each time. I also clarified my usage and why these two methods were being compared since they do function very differently.

John-David Dalton commented :

@Aaron Cool.

I've been told that delete in JavaScript is an expensive operation.

At the language nitty gritty yes, it may also de-opt the object for its lifetime but that's probably a micro-issue. You are right that this language level operation will beat a heavier handed utility method.