| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
|
Is for...of as fast as classic for now? (It probably doesn't matter in this case) |
Sorry, something went wrong.
|
When test count is small (TEST_COUNT = Math.pow(10, 2)), forOf is faster forLoop: 0.205ms forOf: 0.107ms When test count is bigger (TEST_COUNT = Math.pow(10, 8)), forLoop is faster forLoop: 884.075ms forOf: 1629.238ms 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/npmTest 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)) |
Sorry, something went wrong.
|
@gengjiawen turning around the execution order is also going to turn around the results for what is faster for big n. Therefore this benchmark does not seem reliable. 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. |
Sorry, something went wrong.
Sorry, something went wrong.
PR-URL: nodejs#26354 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Beth Griggs <Bethany.Griggs@uk.ibm.com>
PR-URL: nodejs#26354 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Beth Griggs <Bethany.Griggs@uk.ibm.com>
PR-URL: #26354 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Beth Griggs <Bethany.Griggs@uk.ibm.com>
| Back | FazBrowse Home | New Git URL |
Checklist