|
| 1 | +/* performance benchmark */ |
| 2 | +/* This script expects node.js */ |
| 3 | + |
| 4 | +"use strict"; |
| 5 | + |
| 6 | +const { TypedFastBitSet, SparseTypedFastBitSet } = require("../lib"); |
| 7 | +const Benchmark = require("benchmark"); |
| 8 | +const os = require("os"); |
| 9 | + |
| 10 | +const smallgap = 3; |
| 11 | +const largegap = 210; |
| 12 | +const N = 1024 * 1024; |
| 13 | +function ForEachBench() { |
| 14 | + console.log("starting forEach benchmark"); |
| 15 | + const tb = new TypedFastBitSet(); |
| 16 | + const stb = new SparseTypedFastBitSet(); |
| 17 | + |
| 18 | + const s = new Set(); |
| 19 | + for (let i = 0; i < N; i++) { |
| 20 | + tb.add(smallgap * i + 5); |
| 21 | + stb.add(smallgap * i + 5); |
| 22 | + } |
| 23 | + |
| 24 | + const suite = new Benchmark.Suite(); |
| 25 | + // add tests |
| 26 | + const ms = suite |
| 27 | + .add("TypedFastBitSet-forof", function () { |
| 28 | + let card = 0; |
| 29 | + for (const element of stb) { |
| 30 | + card++; |
| 31 | + } |
| 32 | + return card; |
| 33 | + }) |
| 34 | + .add("TypedFastBitSet-foreach", function () { |
| 35 | + let card = 0; |
| 36 | + const inc = function () { |
| 37 | + card++; |
| 38 | + }; |
| 39 | + tb.forEach(inc); |
| 40 | + return card; |
| 41 | + }) |
| 42 | + .add("SparseTypedFastBitSet-forof", function () { |
| 43 | + let card = 0; |
| 44 | + for (const element of stb) { |
| 45 | + card++; |
| 46 | + } |
| 47 | + return card; |
| 48 | + }) |
| 49 | + .add("SparseTypedFastBitSet-foreach", function () { |
| 50 | + let card = 0; |
| 51 | + const inc = function () { |
| 52 | + card++; |
| 53 | + }; |
| 54 | + tb.forEach(inc); |
| 55 | + return card; |
| 56 | + }) |
| 57 | + // add listeners |
| 58 | + .on("cycle", function (event) { |
| 59 | + console.log(String(event.target)); |
| 60 | + }) |
| 61 | + // run async |
| 62 | + .run({ async: false }); |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | +const main = function () { |
| 67 | + console.log( |
| 68 | + "Platform: " + process.platform + " " + os.release() + " " + process.arch |
| 69 | + ); |
| 70 | + console.log(os.cpus()[0]["model"]); |
| 71 | + console.log( |
| 72 | + "Node version " + |
| 73 | + process.versions.node + |
| 74 | + ", v8 version " + |
| 75 | + process.versions.v8 |
| 76 | + ); |
| 77 | + console.log(""); |
| 78 | + ForEachBench(); |
| 79 | + console.log(""); |
| 80 | +}; |
| 81 | + |
| 82 | +if (require.main === module) { |
| 83 | + main(); |
| 84 | +} |
0 commit comments