1

When dealing with huge arrays, and you want to filter elements out of the array, in terms of performance, is it better to create a new array and just push the ones you want to keep in that new array, or manually remove elements from the existing array using something like this Deleting array elements in JavaScript - delete vs splice

Does anyone know?

Thanks

Community
  • 1
  • 1
omega
  • 40,311
  • 81
  • 251
  • 474

4 Answers4

0

creating a new array and pushing consume more time and also memory. But deleting will not take much time and free memory.
If you create a new array and push, it has to compare each and every element and then push.
If you delete, it has to only compare and delete.
Second option is good for performance

Sagar V
  • 12,158
  • 7
  • 41
  • 68
  • If you delete the middle element, does it not have to push all further elements 1 spot behind? – omega Feb 24 '17 at 06:02
  • yes. But in other way, it has to perform n pushes. in this case, only n/2 pushes are required. If the element to be removed is first element, any method can be used. otherwise, second method is good – Sagar V Feb 24 '17 at 06:18
  • Do you mean "delete" as in the `delete` keyword mentioned in the question, or as in `.splice()` (also mentioned in the question)? Because `delete` has some obvious disadvantages, starting with the fact that it doesn't alter the array's `.length`... – nnnnnn Feb 24 '17 at 06:35
  • I didn't mention delete as a keyword. I use it in literal meaning. splice should be used – Sagar V Feb 24 '17 at 06:37
0

You can use splice() to delete an item from the array. consider the below example, if you want to delete the item 3 of index of 2 you can use splice(position,range)

var a = [1,2,3,4,5,6,7];
a.splice(2,1);
console.log(a);

This will output

[1, 2, 4, 5, 6, 7]
Jijo Cleetus
  • 2,679
  • 1
  • 11
  • 17
  • 1
    The question isn't asking how to use `.splice()`, it is asking whether `.splice()` is the best option for performance, as compared to creating a new array populated with the items that are to be kept. – nnnnnn Feb 24 '17 at 06:32
0

The answer should be able to be determined definitively, without speculation, by running the tests using different array methods, or other approaches.

var arr = Array(7);

var r = 3;

console.time(".splice()");

arr.splice(r, 1); // try different methods here

console.timeEnd(".splice()");


var arr = Array(7);

console.time(".filter()");

arr = arr.filter(function(_, i) { // try different methods here
  return i !== r
});

console.timeEnd(".filter()");

var arr = Array(1000);

var r = 500;

console.time(".splice()");

arr.splice(r, 1);

console.timeEnd(".splice()");


var arr = Array(1000);

console.time(".filter()");

arr = arr.filter(function(_, i) {
  return i !== r
});

console.timeEnd(".filter()");

var arr = Array(100000);

var r = 50000;

console.time(".splice()");

arr.splice(r, 1);

console.timeEnd(".splice()");


var arr = Array(100000);

console.time(".filter()");

arr = arr.filter(function(_, i) {
  return i !== r
});

console.timeEnd(".filter()");
guest271314
  • 1
  • 15
  • 104
  • 177
  • @omega At browser `console`, `.splice()` consistently returns results in less time than `.filter()`. Not sure how `delete` is related, unless expected result is `undefined` being substituted for value at the index within array where previous value was set? Which other methods or should be compared? Note, `.splice()` returned results in less time than `.slice()` as well. – guest271314 Feb 24 '17 at 06:53
0

You can use splice method to modify the content of array. If you want to add an element use -

arrayName.splice(index,range,element);

If you want to remove an element use -

arrayName.splice(index,deleteCount);
Rudraksh Pathak
  • 888
  • 8
  • 20
  • 2
    The question isn't asking how to use `.splice()`, it is explicitly asking whether that performs better than creating a new array via some other method. – nnnnnn Feb 24 '17 at 06:30