Created
September 1, 2014 20:23
-
-
Save ailabs-software/50d9af4256253356ebf1 to your computer and use it in GitHub Desktop.
Optimizing removal of elements at positions, when list of positions is greater than 100,000 and array is >250,000.
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
function filterOutPositions(array, positions) @param -- positions to remove | |
{ | |
var BE_DELETED = {}; | |
var arr = array.slice(0); // Clone the array so it can be re-used (non-destructive) | |
var pos_len = positions.length; | |
for (var i=0; i < pos_len; i++) | |
{ | |
arr[ positions[i] ] = BE_DELETED; | |
} | |
// Finally, delete the marked positions | |
for (var ii=arr.length-1; ii >= 0; ii--) | |
{ | |
if ( arr[ii] === BE_DELETED ) | |
{ | |
arr.splice(ii, 1); | |
} | |
} | |
return arr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment