简体   繁体   中英

what is fastest way to remove elements from array of object

I am removing an object from an array and came to know different methods mention below:

let this is my array:

var user = [{id:1}, {id:2}, {id:3}, {id:4}];

First method using splice:

let index = user.findIndex(e=>{
    return e.id == 3;
});
array.splice(index, 1);

Second method using filter:

array = array.filter((data)=>{
   return data.id !== 3;
});

Third method using third party library(like lodash):

_.remove(array, function(data) {
    return data.id == 3 ;
});

which is the fastest among these or is there any more efficient way in terms of performance to remove specific element(which is an object).

If you want to know the execution time of each of options mentioned, you can use -

console.time("Time");
// place your options one by one in between
console.timeEnd("Time");

This will really help with analyzing performance of each piece of code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM