Last active
August 7, 2017 09:48
-
-
Save FLamparski/0708246a4dd0f1f347a4b81461262b8a to your computer and use it in GitHub Desktop.
Iteration stuff (https://jsbench.github.io/#0708246a4dd0f1f347a4b81461262b8a) #jsbench #jsperf
This file contains hidden or 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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"/> | |
| <title>Iteration stuff</title> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script> | |
| <script src="./suite.js"></script> | |
| </head> | |
| <body> | |
| <h1>Open the console to view the results</h1> | |
| <h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2> | |
| </body> | |
| </html> |
This file contains hidden or 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
| "use strict"; | |
| (function (factory) { | |
| if (typeof Benchmark !== "undefined") { | |
| factory(Benchmark); | |
| } else { | |
| factory(require("benchmark")); | |
| } | |
| })(function (Benchmark) { | |
| var suite = new Benchmark.Suite; | |
| Benchmark.prototype.setup = function () { | |
| const nodeExtractionSpecs = [ | |
| { | |
| isValid: obj => obj.type === 'foo', | |
| toNode: obj => ({ key: obj.id, data: obj }), | |
| }, | |
| { | |
| isValid: obj => obj.type === 'bar', | |
| toNode: obj => ({ key: obj.otherId, data: obj }), | |
| }, | |
| ]; | |
| const objs = []; | |
| for (let i = 0; i < 10000; i++) { | |
| objs.push({ | |
| type: !(i % 3) ? 'foo' : 'bar', | |
| [!(i % 3) ? 'id' : 'otherId']: `obj_${i}`, | |
| }); | |
| } | |
| let nodes = new Array(10000); | |
| }; | |
| suite.add("nodeExtractionSpecs.forEach(spec => {", function () { | |
| nodeExtractionSpecs.forEach(spec => { | |
| objs.forEach(obj => { | |
| if (spec.isValid(obj)) { | |
| nodes.push(spec.toNode(obj)); | |
| } | |
| }); | |
| }); | |
| }); | |
| suite.add("for (let j = 0; j < nodeExtractionSpecs.length; j++) {", function () { | |
| for (let j = 0; j < nodeExtractionSpecs.length; j++) { | |
| const spec = nodeExtractionSpecs[j]; | |
| for (let k = 0; k < objs.length; k++) { | |
| if (spec.isValid(objs[k])) { | |
| nodes[k] = spec.toNode(objs[k]); | |
| } | |
| } | |
| } | |
| }); | |
| suite.add("for (const spec of nodeExtractionSpecs) {", function () { | |
| for (const spec of nodeExtractionSpecs) { | |
| for (const obj in objs) { | |
| if (spec.isValid(obj)) { | |
| nodes.push(spec.toNode(obj)); | |
| } | |
| } | |
| } | |
| }); | |
| suite.add("for (let j = nodeExtractionSpecs.length; j--;) {", function () { | |
| for (let j = nodeExtractionSpecs.length; j--;) { | |
| let spec = nodeExtractionSpecs[j]; | |
| for (let k = objs.length; k--;) { | |
| if (spec.isValid(objs[k])) { | |
| nodes[k] = spec.toNode(objs[k]); | |
| } | |
| } | |
| } | |
| }); | |
| suite.add("let j = nodeExtractionSpecs.length;", function () { | |
| let j = nodeExtractionSpecs.length; | |
| while (j--) { | |
| let spec = nodeExtractionSpecs[j]; | |
| let k = objs.length; | |
| while (k--) { | |
| let obj = objs[k]; | |
| if (spec.isValid(obj)) { | |
| nodes[k] = spec.toNode(obj); | |
| } | |
| } | |
| } | |
| }); | |
| suite.add("for (let j = 0; j < nodeExtractionSpecs.length; j++) {", function () { | |
| for (let j = 0; j < nodeExtractionSpecs.length; j++) { | |
| const spec = nodeExtractionSpecs[j]; | |
| for (let k = 0; k < objs.length; k++) { | |
| if (spec.isValid(objs[k])) { | |
| nodes.push(spec.toNode(objs[k])); | |
| } | |
| } | |
| } | |
| }); | |
| suite.add("for (let j = nodeExtractionSpecs.length; j--;) {", function () { | |
| for (let j = nodeExtractionSpecs.length; j--;) { | |
| let spec = nodeExtractionSpecs[j]; | |
| for (let k = objs.length; k--;) { | |
| if (spec.isValid(objs[k])) { | |
| nodes.push(spec.toNode(objs[k])); | |
| } | |
| } | |
| } | |
| }); | |
| suite.add("let j = nodeExtractionSpecs.length;", function () { | |
| let j = nodeExtractionSpecs.length; | |
| while (j--) { | |
| let spec = nodeExtractionSpecs[j]; | |
| let k = objs.length; | |
| while (k--) { | |
| let obj = objs[k]; | |
| if (spec.isValid(obj)) { | |
| nodes.push(spec.toNode(obj)); | |
| } | |
| } | |
| } | |
| }); | |
| suite.on("cycle", function (evt) { | |
| console.log(" - " + evt.target); | |
| }); | |
| suite.on("complete", function (evt) { | |
| console.log(new Array(30).join("-")); | |
| var results = evt.currentTarget.sort(function (a, b) { | |
| return b.hz - a.hz; | |
| }); | |
| results.forEach(function (item) { | |
| console.log((idx + 1) + ". " + item); | |
| }); | |
| }); | |
| console.log("Iteration stuff"); | |
| console.log(new Array(30).join("-")); | |
| suite.run(); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment