5 ms·
> Using a `{}` for this would only work for a set of strings and things that can be uniquely coerced into strings. The code they use has the same limitation. I
by nkoren 11y ago
> Using a `{}` for this would only work for a set of strings and things that can be uniquely coerced into strings.
The code they use has the same limitation. It adds / removes / ensures the uniqueness of elements in the set by using Array.indexOf(...), which only works for strings and things that can be uniquely coerced into strings.
So, given this limitation, using an Object-based set would be vastly more performant.
- dsp1234 11y agovar set = [] var foo = {foot: 1} var bar = {bart: 2} set.push(foo) set.indexOf(foo) //0 set.indexOf(bar) //-1 "indexOf() compares searchElement to elements of the Array using strict equality (the same method used by the ===, or triple-equals, operator)."[0] MDN has the link to the ES5 specification, but the key part is 9(c)(ii): "Let same be the result of applying the Strict Equality Comparison Algorithm to searchElement and elementK." [0] - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- nkoren 11y agoHmm, and yet: var elem = {foo:"bar"} var set = [elem] set.indexOf(elem) // 0 set.indexOf({foo:"bar"}) // -1 So I guess the strict equality comparison is using some kind of internal objectId rather than the property name and values of the objects? I guess depend on how you want to define "uniqueness" for your set...
- dsp1234 11y agoObjects in JavaScript use referential equality when checking for strict equality, which indexOf uses. In your code, 'elem' and '{foo:"bar"}' are different objects which is why your code doesn't work as expected. So var x = {foo:"bar"} var y = {foo:"bar"} var z = x x === y //false x === z //true This is specified in the ES3 document[0] at 11.9.6(13): "13. Return true if x and y refer to the same object or if they refer to objects joined to each other (see 13.1.2). Otherwise, return false." [0] - http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf http://www.ecma-international.org/publications/files/ECMA-ST...
- tlb 11y agoDon't forget to mangle the strings, otherwise inserting things with names meaningful to Javascript ('hasOwnProperty', '__prototype__') can cause great suffering.