-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixes #40: throw when map is mutated in getOrInsertComputed callbacks #73
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. TypeError seems not quite right, but we don't have a better one and it's not worth introducing a new error kind just for this I expect.
Why not just insert a |
The callback could remove and re-add the key, so that wouldn't be sufficient on its own. With how the spec machinery is written I guess you could check to see if the entry record still has the right I expect doing the additional map update would be more expensive for the common case of no mutation in the callback, so that doesn't seem worth it. |
It will make polyfilling of those methods too painful that could slowdown |
@zloirock The polyfill is literally just Map.prototype.getOrInsertComputed = function (key, callback) {
if (typeof callback !== 'function') throw new TypeError;
if (this.has(key)) return this.get(key);
let value = callback(key);
if (this.has(key)) throw new TypeError;
this.set(key, value);
return value;
}; (except with intrinsic versions of There is no reason this should slow down anything else, nor be particularly slow itself. Maybe you are thinking of a different behavior than the one this is actually proposing? This isn't the "lock the map" thing, it's just checking to see if the key was inserted in the map again after calling the callback. |
Ah, in this case, it's fine - after the mentioned issue, I was sure that this option implies map locking that will be added in the future. Sorry -) |
Will note that my optimization note suggestion in #40 (comment) also applies here with little modification. |
Fixes #40. Alternative to #71 (closes #71).
Other currently unexplored options: