Javascript Set vs. Array performance

后端 未结 5 1758
小鲜肉
小鲜肉 2020-11-27 14:03

It maybe because Sets are relatively new to Javascript but I haven\'t been able to find an article, on StackO or anywhere else, that talks about the performance difference b

5条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 14:24

    My observation is that a Set is always better with two pitfalls for large arrays in mind :

    a) The creation of Sets from Arrays must be done in a for loop with a precached length.

    slow (e.g. 18ms) new Set(largeArray)

    fast (e.g. 6ms) const SET = new Set(); const L = largeArray.length; for(var i = 0; i

    b) Iterating could be done in the same way because it is also faster than a for of loop ...

    See https://jsfiddle.net/0j2gkae7/5/

    for a real life comparison to difference(), intersection(), union() and uniq() ( + their iteratee companions etc.) with 40.000 elements

提交回复
热议问题