1

Since JavaScript has no straight concept of a "set", the way I always create an object that acts as a set is to create an associative array where the keys are the elements of the set and the values are true, e.g.

function ToSet ( arr )
{
  // return set of   
  var S = {};
  arr.forEach(function (elem) { S[elem] = true; });
  return S;
}

Since the trues are dummy data, is there a better value to use? Maybe one that only takes up 1 byte?

user5648283
  • 5,913
  • 4
  • 22
  • 32
  • Your set can work fine for items that are all of the same type (all strings or all numbers), but would have issue if you had mixed types like `3` and `"3"`. Also, it won't work for objects as you have it because the default string conversion to get the key will be the same for every object. And, for non-strings, you won't have access to the original value unless you know how to convert the string key back to the original value either. I'd suggest you study this [ES6-like polyfill](https://github.com/jfriend00/ES6-Set/blob/master/set.js) for an idea how to solve some of those issues. – jfriend00 Jan 16 '16 at 06:19
  • Any value will work (including undefined although this would make access harder as an `in` or `hasOwnProperty` would need to be used), but `true` makes the most sense to me - it also makes checking via `S[elem]` and using the value directly unsurprising. Also, this talk about "bytes" is nonsense: implementations of JavaScript are *well optimized* for dealing with the standard undefined/null/true/false - even extending into constrained integers - value set, and one simply can't do better while still using an object-as-a-set. – user2864740 Jan 16 '16 at 06:49

1 Answers1

1

Since JavaScript has no straight concept of a "set"...

Javascript does have a Set object as of ES6. You can read about it here on MDN. And, there are multiple polyfills to use something similar in pre-ES6 environments.

If trying to simulate a Set using a plain Javascript object, the key must be a string so you have to find a unique representation of your object that can be used as a string key. For a number, that's the string representation of the number, for a boolean, that's "true" or "false". For an object, you have to create a unique string that represents the object and if the object is presented again you can recreate the same string. There are multiple possibilities for how to do that in pre-ES6. An ES6 set can hold an object directly without making a string representation of it.

Here's a related answer on using a Set-like object in pre-ES6: Mimicking sets in JavaScript?

And, a derivative of that that aims more for the ES6 Set interface here: https://github.com/jfriend00/ES6-Set.

In this ES6-like interface, there's a function called getKey() here that illustrates a strategy for making a unique string key for many types of ES5 variables. You can see the details there.

As for what to set the data to in the Set, the best bet is to set it to the actual value of the data in the set because the key will be forced to be a string so you need to store the actual value somewhere. So, if the value isn't a string itself, then the actual value can be the value in the object.

Since the trues are dummy data, is there a better value to use? Maybe one that only takes up 1 byte?

As for data size, exactly how Javascript variables are stored is an implementation detail that is not forced by a specification so it can vary from one implementation to another. Because a variable's type has to be part of the value, every variable has to have some sort of universal part of its value. You're not likely to find a value that is smaller than a simple boolean since a type that only has two possible values should be as small as possible already.

But, if you want to be able to tell the difference between a 3 and a "3" stored in your set (e.g. you want them stored as separate items in the set), you will have to add a type modifier to the key so they are stored with separate keys and if you want to be able to get all the values from the set you will need to be able to reconstruct the original value. One simple way to get back to the original value is to store it as the value of the property on the object rather than just true.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979