-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
events: improve for-loop #26354
events: improve for-loop #26354
Conversation
Is |
When test count is small (
When test count is bigger (
Env: System:
OS: macOS 10.14.3
CPU: (8) x64 Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
Memory: 1.27 GB / 16.00 GB
Shell: 3.0.0 - /usr/local/bin/fish
Binaries:
Node: 11.10.1 - /usr/local/bin/node
Yarn: 1.13.0 - /usr/local/bin/yarn
npm: 6.7.0 - /usr/local/bin/npm Test snippet const TEST_COUNT = Math.pow(10, 2);
const pojo = {
name: 'n',
age: 10
}
function Benckmark(desc, func) {
console.time(desc)
let sum = 0;
for (let index = 0; index < TEST_COUNT; index++) {
func()
}
console.timeEnd(desc)
}
const noop = () => {}
function forLoop(o) {
const keys = Object.keys(o)
for (let i = 0; i < keys.length; ++i) {
const key = keys[i]
noop(key)
}
}
function forOf(o) {
for (const key of Object.keys(o)) {
noop(key)
}
}
Benckmark('forLoop', () => forLoop(pojo))
Benckmark('forOf', () => forOf(pojo)) |
@gengjiawen turning around the execution order is also going to turn around the results for what is faster for big The difference between the two loops is not very big but it is ~15%. See https://bugs.chromium.org/p/v8/issues/detail?id=8070 for the tracking bug that I opened last year for it. |
PR-URL: nodejs#26354 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Beth Griggs <[email protected]>
Landed in d79176a 🎉 |
PR-URL: nodejs#26354 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Beth Griggs <[email protected]>
PR-URL: #26354 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Beth Griggs <[email protected]>
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes