Skip to content

events: improve for-loop - #26354

Closed
gengjiawen wants to merge 1 commit into
nodejs:masterfrom
gengjiawen:improve_event_forloop
Closed

events: improve for-loop#26354
gengjiawen wants to merge 1 commit into
nodejs:masterfrom
gengjiawen:improve_event_forloop

Conversation

@gengjiawen

Copy link
Copy Markdown
Member
Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines

@nodejs-github-botnodejs-github-bot added the events Issues and PRs related to the events subsystem / EventEmitter. label Feb 28, 2019
@lpinca

lpinca commented Feb 28, 2019

Copy link
Copy Markdown
Member

Is for...of as fast as classic for now? (It probably doesn't matter in this case)

@gengjiawen

gengjiawen commented Mar 1, 2019

Copy link
Copy Markdown
MemberAuthor

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/npm

Test snippet

constTEST_COUNT=Math.pow(10,2);constpojo={name: 'n',age: 10}functionBenckmark(desc,func){console.time(desc)letsum=0;for(letindex=0;index<TEST_COUNT;index++){func()}console.timeEnd(desc)}constnoop=()=>{}functionforLoop(o){constkeys=Object.keys(o)for(leti=0;i<keys.length;++i){constkey=keys[i]noop(key)}}functionforOf(o){for(constkeyofObject.keys(o)){noop(key)}}Benckmark('forLoop',()=>forLoop(pojo))Benckmark('forOf',()=>forOf(pojo))

@BridgeAR

Copy link
Copy Markdown
Member

@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.

@BridgeAR

Copy link
Copy Markdown
Member

@BridgeARBridgeAR added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Mar 4, 2019
@BridgeARBridgeAR changed the title lib: improve for-loop in events.jsevents: improve for-loopMar 5, 2019
BridgeAR pushed a commit to BridgeAR/node that referenced this pull request Mar 5, 2019
PR-URL: nodejs#26354
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Beth Griggs <Bethany.Griggs@uk.ibm.com>
@BridgeAR

Copy link
Copy Markdown
Member

Landed in d79176a 🎉

@BridgeARBridgeAR closed this Mar 5, 2019
@gengjiawen
gengjiawen deleted the improve_event_forloop branch March 5, 2019 01:13
BridgeAR pushed a commit to BridgeAR/node that referenced this pull request Mar 12, 2019
PR-URL: nodejs#26354
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Beth Griggs <Bethany.Griggs@uk.ibm.com>
BethGriggs pushed a commit that referenced this pull request Apr 16, 2019
PR-URL: #26354
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Beth Griggs <Bethany.Griggs@uk.ibm.com>
@BethGriggsBethGriggs mentioned this pull request May 1, 2019
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author readyPRs that have at least one approval, no pending requests for changes, and a CI started.eventsIssues and PRs related to the events subsystem / EventEmitter.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@gengjiawen@lpinca@BridgeAR@BethGriggs@nodejs-github-bot