JS Drip #47: `delete`

36 views
Skip to first unread message

Aaron Rosenberg

unread,
Dec 10, 2013, 9:45:47 AM12/10/13
to js-drip-d...@googlegroups.com
Great post! One additional thing that I've been told is that `delete` is an expensive operation and should be used sparingly as it will deoptimize the object for it's lifetime (though I'll admit I don't know exactly what the results of that are).  John-David Dalton, one of the contributors to jsperf.com told me this and would certainly know more.

I became interested in this when I was deciding wether to delete a key on an object or use underscore.js' `omit` method on the object to get rid of that key. For this use case `delete` is preferred since it's much faster and the lifetime of the object it is used on is quite short. If you're interested, check out http://jsperf.com/delete-v-omit to see the test and more information about the use case.

Feel free to reply, email, or tweet (@agrberg) any questions.

tomByrer

unread,
Dec 15, 2013, 4:44:13 AM12/15/13
to js-drip-d...@googlegroups.com
That's an interesting study of ES `delete` vs _.omit!  I like to always add in a Lo-Dash test when ever I see an Underscore test, since Dalton tends to make functions that are faster &/or more compatible.  http://jsperf.com/delete-v-omit/2
Lo-Dash is much faster than Underscore in Chrome; I assume Chrome/V8 uses the callback, so you might want to look at that for Node.

I wonder if `omit` might be better for large arrays that become sparse afterwards?  Eg does `delete` release all the unused memory?


On Tuesday, December 10, 2013 7:45:47 AM UTC-7, Aaron Rosenberg wrote:
I became interested in this when I was deciding whether to delete a key on an object or use underscore.js' `omit` method on the object to get rid of that key. For this use case `delete` is preferred since it's much faster and the lifetime of the object it is used on is quite short. If you're interested, check out http://jsperf.com/delete-v-omit to see the test and more information about the use case.

Aaron Rosenberg

unread,
Dec 16, 2013, 9:02:52 AM12/16/13
to js-drip-d...@googlegroups.com
I always forget about Lodash since Underscore is mentioned by my other libraries but if I remember correctly, it should be able to replace underscore in all applications. Very interesting results! `delete` is still the clear winner but in some browsers on some OSes it is more or less tied with lodash while in others performs worse than underscore. While I don't have the time right now, I'm interested in looking into the source code of the two _s to see what they do differently.

Tom Byrer

unread,
Dec 16, 2013, 10:46:18 AM12/16/13
to Aaron Rosenberg, js-drip-d...@googlegroups.com
On Mon, Dec 16, 2013 at 7:02 AM, Aaron Rosenberg <aa...@mojotech.com> wrote:
> I'm interested in looking into the source code of the two _s to see what they do differently.

I assume Lo-Dash in V8 uses a callback.:
https://github.com/lodash/lodash/blob/6ba2730c3fdf33db6f675d222194739b3c39aa22/dist/lodash.compat.js#L3074
https://github.com/jashkenas/underscore/blob/8bb4c9e25a72415918d701f8a43489626e7fc891/underscore.js#L853

Note: Lo-Dash is not a drop-in replacement for Underscore, but
"Underscore build" is.

Joshua Clanton

unread,
Dec 16, 2013, 3:47:28 PM12/16/13
to js-drip-d...@googlegroups.com
Great discussions, guys. I hope we'll see some more like this in the future. The bit about deoptimization is new to me. 

Joshua Clanton

tomByrer

unread,
Dec 16, 2013, 6:23:22 PM12/16/13
to js-drip-d...@googlegroups.com
Thanks Joshua :)

The last few months I'm begging to think that people who work in the web world should of themselves as engineers & not programmers or even 'designers'.   To quote Wikipedia: a web "engineer is concerned with applying scientific knowledge (knowing browsers, HTML, CSS, UA, etc), mathematics (color theory, layout, time-to-glass), and ingenuity to develop solutions for technical problems. Engineers design materials, structures, and systems while considering the limitations imposed by practicality, regulation, (usability), and cost (to program, implement, and use)."

To use `delete` or `_.omit` is a derision an engineering brain must balance the pros & cons.  There is no blanket rule here.

Aaron Rosenberg

unread,
Dec 17, 2013, 9:18:31 AM12/17/13
to js-drip-d...@googlegroups.com
I wish I knew more about the de-optimization but I don't know too much about low level JS stuff like that ... yet :)

I'll agree that we should be engineers instead of developers or designers but I'm also biased since my degree is in engineering and I quit that to do web dev :P.


Reply all
Reply to author
Forward
0 new messages