Skip to content

Instantly share code, notes, and snippets.

@ailabs-software
Created September 1, 2014 20:23
Show Gist options
  • Save ailabs-software/50d9af4256253356ebf1 to your computer and use it in GitHub Desktop.
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.
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