-
Notifications
You must be signed in to change notification settings - Fork 463
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
Add Dict.has & improve Dict helpers performance #7316
Add Dict.has & improve Dict helpers performance #7316
Conversation
@@ -29,7 +29,7 @@ let forEach = (dict, f) => { | |||
} | |||
|
|||
let forEachWithKey = (dict, f) => { | |||
dict->toArray->Stdlib_Array.forEach(((key, value)) => f(value, key)) | |||
dict->keysToArray->Stdlib_Array.forEach(key => f(dict->getUnsafe(key), key)) |
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.
Object.keys(dict).forEach(key => { | ||
let value = dict[key]; |
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.
Ready for review |
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.
Great! Thanks a lot!
target[key] = f(value); | ||
}); | ||
return target; | ||
} | ||
|
||
let has = ((dict, key) => key in dict); |
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.
don't we want to use hasOwnProperty
there?
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.
Yes. There are not many prototype fields on dict object, but still this is invalid with in
:
console.log("__proto__" in {}) // true
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.
Or should we even use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwn ?
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.
I suggest to keep the in
usage. Since it's the fastest and has a decent browser compact. I think there's a really tiny chance for it to become a problem, especially when used on dict. Later, we can move it external
and decided which one to use in compile time.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in
https://jsbenchmark.com/#eyJjYXNlcyI6W3siaWQiOiJWdEx6SzlYNG9fejF0WXQwSG43VWIiLCJjb2RlIjoicmV0dXJuIFwiY29uc3RcIiBpbiBEQVRBIiwiZGVwZW5kZW5jaWVzIjpbXX0seyJpZCI6Ii1xTDJleHRvYlBMM2xHSUw2NVZCdiIsImNvZGUiOiJyZXR1cm4gT2JqZWN0Lmhhc093bihEQVRBLCBcImNvbnN0XCIpIiwiZGVwZW5kZW5jaWVzIjpbXX0seyJpZCI6IjRfbWhmeEpFTGVKTUdGb0J3NXVOaiIsImNvZGUiOiJyZXR1cm4gT2JqZWN0LnByb3RvdHlwZS5oYXNPd25Qcm9wZXJ0eShEQVRBLCBcImNvbnN0XCIpIiwiZGVwZW5kZW5jaWVzIjpbXX0seyJpZCI6InoyR1JEUElmamVzWkRIS2lEaXRrQiIsImNvZGUiOiJyZXR1cm4gREFUQS5oYXNPd25Qcm9wZXJ0eShcImNvbnN0XCIpIiwiZGVwZW5kZW5jaWVzIjpbXX1dLCJjb25maWciOnsibmFtZSI6IkJhc2ljIGV4YW1wbGUiLCJwYXJhbGxlbCI6dHJ1ZSwiZ2xvYmFsVGVzdENvbmZpZyI6eyJkZXBlbmRlbmNpZXMiOltdfSwiZGF0YUNvZGUiOiJyZXR1cm4ge1xuICBjb25zdDogdW5kZWZpbmVkXG59In19
No description provided.