Yup, my "highly scientific" browser benchmark also makes it seem that Set is slower:
Set
function socks_set(arr) { let pairs = 0 const unpaired = new Set() for (const color of arr) if (unpaired.has(color)) { pairs++ unpaired.delete(color) } else unpaired.add(color) return pairs } function socks_obj(ar) { const colors = {} let pairs = 0 for (const color of ar) pairs += !(colors[color] = !colors[color]) return pairs } function* good() { let i = 0 while (i < 100000000) yield i++ >> 1 return i >> 1 } function* bad() { let i = 0 while (i < 10000000) yield i++ return i } console.log(socks_set(good())) console.log(socks_obj(good())) console.time("good Set") socks_set(good()) console.timeEnd("good Set") console.time("good Obj") socks_obj(good()) console.timeEnd("good Obj") console.time("good Set") socks_set(good()) console.timeEnd("good Set") console.time("good Obj") socks_obj(good()) console.timeEnd("good Obj") console.log(socks_set(bad())) console.log(socks_obj(bad())) console.time("bad Set") socks_set(bad()) console.timeEnd("bad Set") console.time("bad Obj") socks_obj(bad()) console.timeEnd("bad Obj") console.time("bad Set") socks_set(bad()) console.timeEnd("bad Set") console.time("bad Obj") socks_obj(bad()) console.timeEnd("bad Obj")
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Yup, my "highly scientific" browser benchmark also makes it seem that
Set
is slower: