-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
While working on some SpiderMonkey JIT compiler optimisations, I was wondering if it makes sense to change ToInteger(-0) to return +0 instead of -0, because in all but one case, the result of ToInteger(-0) is either explicitly or implicitly changed from -0 to +0 anyway.
Example for implicit conversion from String.prototype.startsWith:
- An implicit conversion through the mathematical function
max.
- If endPosition is undefined, let pos be len; else let pos be ? ToInteger(endPosition).
- Let end be min(max(pos, 0), len).
And from String.prototype.charAt:
- An implicit conversion because -0 is treated the same as +0 for index positions.
- Let position be ? ToInteger(pos).
...- Return the String value of length 1, containing one code unit from S, namely the code unit at index position.
And from Array.prototype.includes:
- Implicit conversion, because
ToString(-0) = ToString(+0) = "0"holds.
- Let n be ? ToInteger(fromIndex).
...- If n ≥ 0, then
a. Let k be n.
...- Repeat, while k < len
a. Let elementK be the result of ? Get(O, ! ToString(k)).
Example for explicit conversion from Array.prototype.indexOf:
- Let n be ? ToInteger(fromIndex).
...- If n ≥ 0, then
a. If n is -0, let k be +0; else let k be n.
Or TimeClip:
- Let clippedTime be ! ToInteger(time).
- If clippedTime is -0, set clippedTime to +0.
The only place where changing ToInteger(-0) to return +0 instead of -0 would make a difference, is Atomics.store:
- Let v be ? ToInteger(value).
...- Return v.
So, changing ToInteger(-0) return +0 would allow to remove some explicit -0 to +0 conversions in various operations, but also result in a noticeable change for Atomics.store, (which we could avoid if necessary, but I'm not actually sure anyone would really notice the difference for that function when +0 instead of -0 would be returned.) Does it sound useful to anyone else to make this change?