Search code examples
javascriptobjecthashforeachlodash

javascript object hash vs lodash foreach in lodash remove


I'm trying to delete some data from a complex object.

The format of the original data is as follows.

let originData = 
[
    {
       name : exampleDepth1,
       depth1Data : 
       [
            {
                 name : exampleDepth2,
                 depth2Data : 
                 [
                     {
                         code: 1234///will be delete from that data
                     },
                     ...
                 ]
            },
            ...
        ]
    },
    ....
]

let willbeDeletecode = [ 3, 100, 1234, 1000];

The name of the array to be deleted is the code value in the depth2Data array of originData, The name of the array to delete is willbeDeletecode.

I'm sorry if you were uncomfortable.

I'm trying to erase it in two ways.

let deleteBook = {}
_.forEach(willbeDeletecode, (deleteCode) => {
  deleteBook[`${deleteCode}`] = deleteCode;
})

_.remove(originData, (depth1) => {
  _.remove(depth1.depth1Data, (depth2) => {
    /*
    // delete with object hash
    _.remove(depth2.depth2Data, (eachDepth2Data) => {
      return deleteBook[eachDepth2Data.code] === undefined
    })
    */

    /*
    // delete with forEach
    let ret = false;
     _.remove(depth2.depth2Data, (eachDepth2Data) => {
       _.forEach(willbeDeletecode, (deleteCode) => {
         if(deleteCode === eachDepth2Data.code){
             ret = true;
             return false;
            }
        })
        return ret
    })
    */
    return depth2.depth2Data.length === 0;
  })
  return depth1.depth1Data.length === 0;
})

I have two separate ways of annotating each one.

The first is to create an object(deleteBook) and insert the data of willbeDeletecode and use it in remove of lodash.

The second method is entirely a comparison of all through the forEach function.

The above method was repeated 1000 times to benchmark. As a result, the first method is 100 ~ 200ms and the second method is 500 ~ 700ms.

Of course, the willbeDeletecode is around 10 or so, but I thought Object hash was faster. But the result was the opposite.

If there are more variables in willbeDeletecode, will there be another conclusion? I want know why this results.


Solution

  • The object hash is to be preferred. You could also use an ES6 Set for such purpose.

    Such a hash solution should be faster.

    One reason that you did not see this in your case, is that the first variant of your code removes the opposite of what it should. The _remove callback should return a truthy value when the corresponding item should be removed, yet your code returns true when the value is not in the codes that should be deleted. You should use a !== comparison:

    _.remove(depth2.depth2Data, (eachDepth2Data) => {
        return deleteBook[eachDepth2Data.code] !== undefined
    })
    

    As you had a === there, you probably had a lot more removals going on, giving a longer time of execution.