`#times_kachick channel in chat` as a public repository. Personal Note and TODOs
6
stars
0
forks
source link
2022-05-28 - JavaScript の Array difference を取得する時に `Array.prototype.includes()` を使っている説明例が多いけど、片方を Set にして `Set.prototype.has()` を使ったほうが無難だったりしないのかな #161
Closed
kachick closed 2 years ago
経緯 / History
JavaScript の Array difference (Ruby で言うところの
[1, 2, 3] - [2] #=> [1, 3]
) を取りたい時にググると大抵Array.prototype.includes()
を使っているような StackOverFlow とかが出てくる。 ぱっと見で一番 ⭐ 集めてた https://stackoverflow.com/a/4026828 とかもそう。 しかし実質ループの中でループ回す処理に見えてウッとなる。そういえば Ruby の この辺の処理は確か内部で hash 値? 使った lookup してたし、 Ruby の Set は Hash のラッパー。ということは JavaScript でもこの辺は Set 使うだけでマシになったりしないかな?と思ってちょっと試してみた。ここでは Node.js を使っているけれど、別に Node.js の実装を確認したりはしていない。 一々 npm でベンチマークツール入れてとか面倒なので REPL で雑に 処理が単調なので includes での探索でヒットするまでの回数が固定これぐらい大きな値になると流石に差が激しくて、700倍近い結果が出た。それなりに大きい Array でも Set に変換するコストは小さい感じなので、
array1.filter((v) => array2.includes(v))
みたいなコードを書くときにはちょっと思い出すようにしたいそもそも ECMA Script の proposal にも、Array というか Set に集合演算を用意しようという話は挙がってるみたい。 https://github.com/tc39/proposal-set-methods でも https://github.com/tc39/proposal-set-methods/blob/2e58731d1e88656f1abf1c0e5272ba320258cd30/spec/set-prototype-difference.html#L1-L17 見る限り引数に iterable 受け取る?ということは結局使い方気をつける必要ありそうな・・・?