Last active
December 18, 2015 12:39
-
-
Save frostney/5784526 to your computer and use it in GitHub Desktop.
Safe and fast alternative for deleting items from arrays and objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
deleteItem = (obj, item) -> | |
if Array.isArray obj | |
i for i, num in obj when num isnt item | |
else | |
newObject = {} | |
newObject[key] = obj[key] for key of obj when key isnt item | |
newObject |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Performance tests:
Small array: http://jsperf.com/delete-vs-deleteitem-small-array
Small object: http://jsperf.com/delete-vs-deleteitem-small-object
Big array: http://jsperf.com/delete-vs-deleteitem-big-array
Operation speed: http://jsperf.com/delete-vs-deleteitem-operation-speed