Comparing short array of numbers (v2)

Revision 2 of this benchmark created on


Setup

const randomSmallNumber = (max) => Math.floor(Math.random() * max)

const checkIfSlidesChanged = (before, after) => { 

        if (before.length !== after.length) return true;

        // if any slides don't match, then slides have changed
        if (after.find((slide, idx) => slide !== before[idx])) return true;

        return false;
    };

// comparing 2 very different small arrays
const slidesAfter = [];
const slidesBefore = [];

// random small array
for (let i=0; i < randomSmallNumber(10) + 1; i++) {
	slidesAfter.push(randomSmallNumber(30));
	slidesBefore.push(randomSmallNumber(30));
}

// short circuit 10% of the time
if (randomSmallNumber(9) === 1) {
	slidesBefore.push(randomSmallNumber(30));
}

// comparing 2 small arrays with only a single difference
const similarSlidesBefore = [...slidesBefore];
const similarSlidesAfter = [...slidesBefore];


const idx = randomSmallNumber(similarSlidesBefore.length - 1);
similarSlidesBefore[idx] = randomSmallNumber(30);

Test runner

Ready to run.

Testing in
TestOps/sec
JSON - Very Different Arrays
JSON.stringify(slidesBefore) === JSON.stringify(slidesAfter);
ready
Iteration - Very Different Arrays
checkIfSlidesChanged(slidesBefore, slidesAfter);
ready
JSON - Very Similar Arrays
JSON.stringify(similarSlidesBefore) === JSON.stringify(similarSlidesAfter);
ready
Iteration - Very Similar Arrays
checkIfSlidesChanged(similarSlidesBefore, similarSlidesAfter);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.