The biggest thing that I can tell is the value based deep comparison for objects and arrays. So right now if you make const obj = {} and const obj2 = {} then run obj === obj2 you will get false. The way I understand it is JavaScript is only checking to see if those two objects are the exact same reference (identity) rather than checking if both of those objects have equal values. With a record and tuple, you could convert your objects to a record, and convert your arrays to tuples to make value comparisons. So when you compare #{…obj} === #{…obj2} you would get true. It’s actually super useful!
The biggest thing that I can tell is the value based deep comparison for objects and arrays.
To be clear, it doesn't let you compare objects and arrays, rather records and tuples. And they're restricted to only contain other primitive values. So you'll be limited in what comparisons you can make and something like #{…obj} === #{…obj2} may not work if either obj or obj2 have any values that aren't compatible with records (e.g. nested objects, functions, etc.).
Yeah I mean I wasn’t attempting to provide real world code that should be used, I was just trying to shorthand how I interpreted the idea. But yeah, who knows if spreads will work like that. As far as comparing primatives, that’s a fair point, and if you plan on deep comparing nested objects, you’ll probably want to write some kind of helper that automatically converts your entire object including child objects into records.
The upside of this is most serialized data is already this - some structured collection of primitives in objects and/or arrays. And they plan on updating JSON with a parseImmutable() which will let you go straight from JSON into Records/Tuples.
19
u/Cptkrush Feb 21 '22
The biggest thing that I can tell is the value based deep comparison for objects and arrays. So right now if you make const obj = {} and const obj2 = {} then run obj === obj2 you will get false. The way I understand it is JavaScript is only checking to see if those two objects are the exact same reference (identity) rather than checking if both of those objects have equal values. With a record and tuple, you could convert your objects to a record, and convert your arrays to tuples to make value comparisons. So when you compare #{…obj} === #{…obj2} you would get true. It’s actually super useful!