Last active
September 15, 2015 07:36
-
-
Save davidbarredo/020019e4f2d23f8eb6ed to your computer and use it in GitHub Desktop.
Empty an array methods
This file contains 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
A = []; | |
// This is perfect if you don't have references to the original array A anywhere else because this actually creates a brand new (empty) array. | |
A.length = 0; | |
// This will clear the existing array by setting its length to 0 | |
A.splice(0,A.length); | |
// Using .splice() will work perfectly, but since .splice() function will return an array with all the removed items, it will actually return a copy of the original array. Benchmarks suggests that this has no effect on performance whatsoever. | |
while(A.length > 0) { | |
A.pop(); | |
}; | |
// This solution is not very succinct and it is also the slowest solution |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From the stackoverflow answer: http://stackoverflow.com/a/1232046