Skip to content

Instantly share code, notes, and snippets.

@imedadel
Created April 23, 2020 11:23
Show Gist options
  • Save imedadel/ac01110c17faf076ed91b5d1869bf393 to your computer and use it in GitHub Desktop.
Save imedadel/ac01110c17faf076ed91b5d1869bf393 to your computer and use it in GitHub Desktop.
// taken from https://jsperf.com/array-remove-by-index
// splice: 6,957,292 ops/sec
// swap: 85,501,603 ops/sec (NB. changes order)
// shift: 70,903,814 ops/sec
// btw. don't actually modify the Array prototype. Create a separate function instead.
Array.prototype.mySwapDelete = function arrayMySwapDelete(index) {
this[index] = this[this.length - 1];
this.pop();
};
Array.prototype.myShiftDelete = function arrayMyShiftDelete(index) {
var stop = this.length - 1;
while (index < stop) {
this[index] = this[++index];
}
this.pop();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment