Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames M Snell <jasnell@gmail.com>2017-02-26 09:06:31 +0300
committerItalo A. Casas <me@italoacasas.com>2017-03-20 21:12:06 +0300
commitdcac2d8f04663548fe10cabff47cca567aedc895 (patch)
tree2a39e74e1a9b759161afdbe251a2e54f0779af47 /benchmark
parentd0fb578d6499a5fc6e6189c6239b9a75dbaedce0 (diff)
benchmark: benchmark comparing forEach with for
PR-URL: https://github.com/nodejs/node/pull/11582 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'benchmark')
-rw-r--r--benchmark/es/foreach-bench.js82
1 files changed, 82 insertions, 0 deletions
diff --git a/benchmark/es/foreach-bench.js b/benchmark/es/foreach-bench.js
new file mode 100644
index 00000000000..fea5318bc66
--- /dev/null
+++ b/benchmark/es/foreach-bench.js
@@ -0,0 +1,82 @@
+'use strict';
+
+const common = require('../common.js');
+
+const bench = common.createBenchmark(main, {
+ method: ['for', 'for-of', 'for-in', 'forEach'],
+ count: [5, 10, 20, 100],
+ millions: [5]
+});
+
+function useFor(n, items, count) {
+ var i, j;
+ bench.start();
+ for (i = 0; i < n; i++) {
+ for (j = 0; j < count; j++) {
+ /* eslint-disable no-unused-vars */
+ var item = items[j];
+ /* esline-enable no-unused-vars */
+ }
+ }
+ bench.end(n / 1e6);
+}
+
+function useForOf(n, items) {
+ var i, item;
+ bench.start();
+ for (i = 0; i < n; i++) {
+ for (item of items) {}
+ }
+ bench.end(n / 1e6);
+}
+
+function useForIn(n, items) {
+ var i, j, item;
+ bench.start();
+ for (i = 0; i < n; i++) {
+ for (j in items) {
+ /* eslint-disable no-unused-vars */
+ item = items[j];
+ /* esline-enable no-unused-vars */
+ }
+ }
+ bench.end(n / 1e6);
+}
+
+function useForEach(n, items) {
+ var i;
+ bench.start();
+ for (i = 0; i < n; i++) {
+ items.forEach((item) => {});
+ }
+ bench.end(n / 1e6);
+}
+
+function main(conf) {
+ const n = +conf.millions * 1e6;
+ const count = +conf.count;
+
+ const items = new Array(count);
+ var i;
+ var fn;
+ for (i = 0; i < count; i++)
+ items[i] = i;
+
+ switch (conf.method) {
+ case 'for':
+ fn = useFor;
+ break;
+ case 'for-of':
+ fn = useForOf;
+ break;
+ case 'for-in':
+ fn = useForIn;
+ break;
+ case 'forEach':
+ fn = useForEach;
+ break;
+ default:
+ throw new Error('Unexpected method');
+ }
+ fn(n, items, count);
+}