Suggest Object.compare(lert, right)
This may have already been proposed as sebmarkbage/ecmascript-shallow-equal - if so, you'll want to post your comments there.
Well, this proposal is not for check equality. Its purpose is to compare
two object that which object is greater
.
I'm sure this feature is already implemented for every modern js engines, as it's needed to implement Map/Set. But we don't have js interface for it yet. Thus proposal is to expose this functionality to userland application.
How is it needed for Map/Set? Those use SameValueZero, and don't have a sorting method.
Oh yeah, I missed that hashtable ordering is undeterministic. I played a lot with trees these days.
Anyway, how's the proposal itself?
(Wow, I got really confused how this mail thread works)
On Mon, Sep 19, 2016 at 7:32 AM, Park Hyeonu <nemo1275 at gmail.com> wrote:
I'm sure this feature is already implemented for every modern js engines, as it's needed to implement Map/Set.
But you know that, it's indeterministic and implementation is free to reorganize pointers?
a = {}; b = {}; Object.compare(a,b); // 1 gc.enforceGC(); Object.compare(a,b); // -1
So any userland structure can't rely on persistence of order.
Moreover, such ordering can be implemented with WeakMap.
const wm = new WeakMap() let i = 0; function getId(obj) { if (!wm.has(obj)) wm.set(obj, i++); return wm.get(obj); }
I'm definitely not a js engine expert but isn't there some pointer-of-pointer thingy that points rearranged position(maybe called handle)?
I leaved the exact comparing rule as black box and let engines care such cases. The single rule is that it should be deterministic.
Anyway, your WeakMap-based implementation can easily exceeds Number.MAX_SAFE_INTEGER with some long-running application, as numbers cannot be reused.
-
-
- 오후 6:03에 "Michał Wadas" <michalwadas at gmail.com>님이 작성:
-
If you call such implementation million times per second, you would need ~280 years to exhaust 2^53 values. So it's definitely a long running program.
Um, then it's reasonable implementation. But it can be far much optimized with native implementation(naively say, pointer comparison?).
Anyway, we should not decide to include something in spec because it can/cannot be implemented. And this proposal can make additional possibility to js libraries.
For example, facebook/ImmutableJS currently uses array-based O(n) approach for it's Map/Set structure. Because there's still no way to compare arbitrary objects in js. Yes, O(n) for search!
-
-
- 오후 6:44에 "Michał Wadas" <michalwadas at gmail.com>님이 작성:
-
tl;dr - deterministic function that compares arbitrary objects
I know now we have
Map
andSet
, but sometimes we need more low level ones. Especially for library developers.For the case, say, I want to implement a map whose key can be a arbitrary number of objects. Currently this can only implemented as nested map whose structure is
keylen -> key1 -> key2 -> ... -> value
. Not really elegant.But if we have
Object.compare()
,(key1, key2, ...) -> value
is possible.As its intended purpose is to be used with custom map/set, I don't think we should define how this function works in spec. Just guaranteed to be deterministic in each runtime is enough.
For detail, this function follows
(left: any, right: any) => number
form.When
Object.is(left, right)
is true,Object.compare(left, right)
must return0
. Otherwise, the result must be deterministic, means whenever(in same runtime) we call this function with same arguments it should return same value(or at least, always greater than/lesser 0).Seems good?