Difference between find() and filter().shift() on javascript

后端 未结 2 1969
梦毁少年i
梦毁少年i 2020-12-08 00:12

I recently starting to drop underscore/lodash overuse on (some of) my projects and realize that there\'s no full support of find method in browsers. What\'s the difference b

2条回答
  •  感情败类
    2020-12-08 00:38

    Use a polyfill; users.filter(function() { ... } ).shift(); is throwing cycles away while triggering unnecessary garbage collection.

    • filter scans the entire array and creates another array
    • shift now has to change all the indexes on the temp array

    A slightly less wasteful pattern would be to use users.filter(function() { ... } )[0]

提交回复
热议问题