forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
benchmark: add benchmark for EventTarget add and remove
Refs: nodejs/performance#60
- Loading branch information
1 parent
bc58a85
commit 102712b
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
const common = require('../common.js'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [1e6], | ||
nListener: [5, 10], | ||
}); | ||
|
||
function main({ n, nListener }) { | ||
const target = new EventTarget(); | ||
const listeners = []; | ||
for (let k = 0; k < nListener; k += 1) | ||
listeners.push(() => {}); | ||
|
||
bench.start(); | ||
for (let i = 0; i < n; i += 1) { | ||
const dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1'; | ||
for (let k = listeners.length; --k >= 0;) { | ||
target.addEventListener(dummy, listeners[k]); | ||
} | ||
for (let k = listeners.length; --k >= 0;) { | ||
target.removeEventListener(dummy, listeners[k]); | ||
} | ||
} | ||
bench.end(n); | ||
} |