Skip to content

Instantly share code, notes, and snippets.

@davidbarredo
Last active September 15, 2015 07:36
Show Gist options
  • Save davidbarredo/020019e4f2d23f8eb6ed to your computer and use it in GitHub Desktop.
Save davidbarredo/020019e4f2d23f8eb6ed to your computer and use it in GitHub Desktop.
Empty an array methods
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
@davidbarredo
Copy link
Author

From the stackoverflow answer: http://stackoverflow.com/a/1232046

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment