javascript object hash vs lodash foreach in lodash remove












1















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.










share|improve this question





























    1















    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.










    share|improve this question



























      1












      1








      1








      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.










      share|improve this question
















      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.







      javascript object hash foreach lodash






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 7:51









      Rajendra arora

      1,50211018




      1,50211018










      asked Jan 2 at 6:14









      naiadnaiad

      809




      809
























          1 Answer
          1






          active

          oldest

          votes


















          0














          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.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002017%2fjavascript-object-hash-vs-lodash-foreach-in-lodash-remove%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            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.






            share|improve this answer




























              0














              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.






              share|improve this answer


























                0












                0








                0







                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.






                share|improve this answer













                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 5 at 7:50









                trincottrincot

                127k1688123




                127k1688123
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002017%2fjavascript-object-hash-vs-lodash-foreach-in-lodash-remove%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Mossoró

                    Error while reading .h5 file using the rhdf5 package in R

                    Pushsharp Apns notification error: 'InvalidToken'