Skip to content

Instantly share code, notes, and snippets.

@jeanlucaslima
Last active May 5, 2017 05:42
Show Gist options
  • Select an option

  • Save jeanlucaslima/04144258d421177f21c41f26b14aa607 to your computer and use it in GitHub Desktop.

Select an option

Save jeanlucaslima/04144258d421177f21c41f26b14aa607 to your computer and use it in GitHub Desktop.

Revisions

  1. jeanlucaslima revised this gist May 5, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion weird.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    // Estranho bug de performance encontrado por [andreasgal](https://github.com/andreasgal)
    //
    // Comportamento esperado: foo() e bar() terem ao menos performance similar
    // Comportamento observado: foo() tem performance até 4x maior que bar() depndendo da máquina
    // Comportamento observado: foo() tem performance diferente que bar() dependendo da máquina
    //
    // A única diferença entre foo() e bar() é a ordem de declaração dos valores dentro do vetor
    // Não sei explicar o motivo desse comportamento
  2. jeanlucaslima revised this gist May 5, 2017. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion weird.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,9 @@
    // Estranho bug de performance encontrado por [andreasgal](https://github.com/andreasgal)
    // A única diferença entre foo() e bar() é a ordem de declaração do vetor
    //
    // Comportamento esperado: foo() e bar() terem ao menos performance similar
    // Comportamento observado: foo() tem performance até 4x maior que bar() depndendo da máquina
    //
    // A única diferença entre foo() e bar() é a ordem de declaração dos valores dentro do vetor
    // Não sei explicar o motivo desse comportamento

    var A = [];
  3. jeanlucaslima revised this gist May 5, 2017. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions weird.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    // Estranho bug de performance encontrado por [andreasgal](https://github.com/andreasgal)
    // A única diferença entre foo() e bar() é a ordem de declaração do vetor
    // Não sei explicar o motivo desse comportamento

    var A = [];
    function foo() {
    var J = [124, 564, 456, 1000];
  4. jeanlucaslima revised this gist May 5, 2017. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion weird.js
    Original file line number Diff line number Diff line change
    @@ -14,5 +14,15 @@ function bar() {
    }
    }

    var perf_foo_t0 = performance.now();
    foo();
    bar();
    var perf_foo_t1 = performance.now();
    var perf_bar_t0 = performance.now();
    bar();
    var perf_bar_t1 = performance.now();

    console.log("Call to bar took " + (perf_bar_t1 - perf_bar_t0) + " milliseconds.");
    console.log("Call to foo took " + (perf_foo_t1 - perf_foo_t0) + " milliseconds.");

    console.log("foo was " + (perf_foo_t1 - perf_foo_t0)/(perf_bar_t1 - perf_bar_t0) + " times faster than bar.");

  5. jeanlucaslima revised this gist May 5, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion weird.js
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ function foo() {

    var B = [];
    function bar() {
    var K = [1000, 564, 456];
    var K = [1000, 124, 564, 456];
    for (var j = 0; j < 5000000; j++) {
    B[K[j % 4]] = j;
    }
  6. jeanlucaslima created this gist May 5, 2017.
    18 changes: 18 additions & 0 deletions weird.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    var A = [];
    function foo() {
    var J = [124, 564, 456, 1000];
    for (var i = 0; i < 5000000; i++) {
    A[J[i % 4]] = i;
    }
    }

    var B = [];
    function bar() {
    var K = [1000, 564, 456];
    for (var j = 0; j < 5000000; j++) {
    B[K[j % 4]] = j;
    }
    }

    foo();
    bar();