Why is jest-bench setup() function impacting 'for loop' performance / JS behavior?

15 views Asked by At

I was testing using jsbench.me, but decided to try jest-bench and I've created below tests. Since it is my first test I've tried with and without setup() function. Results differences are quite shocking. jest-bench results, without a setup function, are very much aligned with jsbench.me, so the question would be why is setup function impacting the for loop, so much?.

I'd expected the setup() function to have no impact on the results.

Should I even use jest-bench instead of benchmark.js ? Results from jest-bench.

Benchmarks:
  loop vs forEach with Setup
    forEach   2,178,790 ops/sec  0.459 μs ±  2.62 %  (85 runs sampled)
    for loop     29,197 ops/sec  0.034 ms ±  0.97 %  (95 runs sampled)

  loop vs forEach with module level variables
    forEach   3,428,354 ops/sec  0.292 μs ±  1.77 %  (85 runs sampled)
    for loop  7,223,721 ops/sec  0.138 μs ±  1.02 %  (92 runs sampled)    

Results from same test at jsbench.me

    forEach:  3,2 mln ops/s ± 2.33%
    for loop: 6,9 mln ops/s ± 1.62%

Without setup(). Using module variables

import { benchmarkSuite } from "jest-bench";

let mockDimmensioningData = [];

for (let i = 0; i < 200; i++) {
    mockDimmensioningData.push({
        side: 'up',
        coords: { x: i, y: -i },
        distance: i
    });
} 

benchmarkSuite("loop vs forEach with module level 'setup'", {

    ["forEach"]: () => {
        let distance = 0;
        let prevPoint = { x: 0, y: 0 };
        mockDimmensioningData.forEach((dimmension) => {
            distance = dimmension.distance - distance;
            prevPoint = dimmension.coords;
        });
    },

    ["for loop"]: () => {
        let distance = 0;
        let prevPoint = { x: 0, y: 0 };
        ;
        for (let index =0; index < mockDimmensioningData.length; index++) {
            distance = mockDimmensioningData[index].distance - distance;
            prevPoint = mockDimmensioningData[index].coords;
        }
    },
});

using jest-bench setup()

import { benchmarkSuite } from "jest-bench";
 
benchmarkSuite("loop vs forEach with Setup", {
    setup() {
        mockDimmensioningData = [];

        for (let i = 0; i < 200; i++) {
            mockDimmensioningData.push({
                side: 'up',
                coords: { x: i, y: -i },
                distance: i
            });
        }
    },

    ["forEach"]: () => {
        let distance = 0;
        let prevPoint = { x: 0, y: 0 };
        mockDimmensioningData.forEach((dimmension) => {
            distance = dimmension.distance - distance;
            prevPoint = dimmension.coords;
        });
    },

    ["for loop"]: () => {
        let distance = 0;
        let prevPoint = { x: 0, y: 0 };

        for (let index = 0; index < mockDimmensioningData.length; index++) {
            distance = mockDimmensioningData[index].distance - distance;
            prevPoint = mockDimmensioningData[index].coords;
        }
    },
});

I've included benchmark.js tag since jest-bench is built on top of it. Best regards Bart

0

There are 0 answers