Skip to content

Instantly share code, notes, and snippets.

@warpech
Last active November 6, 2019 09:18

Revisions

  1. warpech revised this gist Nov 6, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion suite.js
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@
    }

    /*
    Conclusion: Naive arrays are faster for reads than preallocated, because naive are PACKED and preallocated are HOLEY
    Conclusion (Chrome & FF): Naive arrays are faster for reads than preallocated, because naive are PACKED and preallocated are HOLEY
    See:
    https://github.com/babel/babel/issues/6233
  2. warpech revised this gist Nov 6, 2019. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions suite.js
    Original file line number Diff line number Diff line change
    @@ -48,10 +48,6 @@
    }
    });

    suite.add("", function () {

    });

    suite.on("cycle", function (evt) {
    console.log(" - " + evt.target);
    });
  3. warpech revised this gist Nov 6, 2019. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions suite.js
    Original file line number Diff line number Diff line change
    @@ -26,8 +26,9 @@

    suite.add("naive", function () {
    // naive
    let result;
    for(let i = 0; i < length; i++) {
    naiveArray.push(true);
    result = naiveArray[i];
    }

    /*
    @@ -41,8 +42,9 @@

    suite.add("preallocated", function () {
    // preallocated
    let result;
    for(let i = 0; i < length; i++) {
    preallocatedArray[i] = true;
    result = preallocatedArray[i];
    }
    });

  4. warpech revised this gist Nov 6, 2019. 2 changed files with 10 additions and 5 deletions.
    2 changes: 1 addition & 1 deletion index.html
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    <html>
    <head>
    <meta charset="utf-8"/>
    <title>Array read</title>
    <title>Array read #jsbench #jsperf</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
    <script src="./suite.js"></script>
    </head>
    13 changes: 9 additions & 4 deletions suite.js
    Original file line number Diff line number Diff line change
    @@ -29,6 +29,14 @@
    for(let i = 0; i < length; i++) {
    naiveArray.push(true);
    }

    /*
    Conclusion: Naive arrays are faster for reads than preallocated, because naive are PACKED and preallocated are HOLEY
    See:
    https://github.com/babel/babel/issues/6233
    https://v8.dev/blog/elements-kinds
    */
    });

    suite.add("preallocated", function () {
    @@ -39,10 +47,7 @@
    });

    suite.add("", function () {
    /*
    Conclusion: Naive arrays are faster for reads than preallocated, because naive are PACKED and preallocated are HOLEY

    */
    });

    suite.on("cycle", function (evt) {
    @@ -61,7 +66,7 @@
    });
    });

    console.log("Array read");
    console.log("Array read #jsbench #jsperf");
    console.log(new Array(30).join("-"));
    suite.run();
    });
  5. warpech created this gist Nov 6, 2019.
    13 changes: 13 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8"/>
    <title>Array read</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>
    67 changes: 67 additions & 0 deletions suite.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    "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 length = 10000;

    const naiveArray = [];
    for(let i = 0; i < length; i++) {
    naiveArray.push(true);
    }

    const preallocatedArray = new Array(length);
    for(let i = 0; i < length; i++) {
    preallocatedArray[i] = true;
    }
    };


    suite.add("naive", function () {
    // naive
    for(let i = 0; i < length; i++) {
    naiveArray.push(true);
    }
    });

    suite.add("preallocated", function () {
    // preallocated
    for(let i = 0; i < length; i++) {
    preallocatedArray[i] = true;
    }
    });

    suite.add("", function () {
    /*
    Conclusion: Naive arrays are faster for reads than preallocated, because naive are PACKED and preallocated are HOLEY
    */
    });

    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("Array read");
    console.log(new Array(30).join("-"));
    suite.run();
    });