Last active
July 9, 2020 17:45
-
-
Save baptistemanson/b2e1dd24022b493511203a217a215c39 to your computer and use it in GitHub Desktop.
How many collision can I test in JS per frame.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isInside(pos, rect) { | |
return ( | |
pos[0] > rect[0] && pos[1] > rect[1] && pos[0] < rect[2] && pos[1] < rect[2] | |
); | |
} | |
const rect1 = new Uint16Array([10, 10, 100, 1000]); | |
const rect2 = new Uint16Array([0, 0, 10, 10]); | |
const pos1 = new Uint16Array([250, 50]); | |
const pos2 = new Uint16Array([20, 20]); | |
const pos3 = new Uint16Array([20, 90]); | |
const pos1a = [250, 50]; | |
const pos2a = [20, 20]; | |
const pos3a = [20, 90]; | |
const rect1a = [0, 0, 10, 10]; | |
const rect2a = [10, 10, 100, 1000]; | |
const Benchmark = require("benchmark"); | |
const suite1 = new Benchmark.Suite(); | |
suite1 | |
.add("typed arrays", function () { | |
isInside(pos1, rect1); | |
isInside(pos1, rect2); | |
isInside(pos2, rect1); | |
isInside(pos2, rect2); | |
isInside(pos3, rect1); | |
}) | |
.add("typed arrays mixed with arrays", function () { | |
isInside(pos1, rect1a); | |
isInside(pos1, rect2a); | |
isInside(pos2, rect1a); | |
isInside(pos2, rect2a); | |
isInside(pos3, rect1a); | |
}) | |
.add("arrays", function () { | |
isInside(pos1a, rect1a); | |
isInside(pos1a, rect2a); | |
isInside(pos2a, rect1a); | |
isInside(pos2a, rect2a); | |
isInside(pos3a, rect1a); | |
}) | |
.on("complete", function () { | |
for (var i = 0; i < this.length; i++) { | |
console.log( | |
this[i].name + | |
" " + | |
((this[i].hz * 5) / 60).toLocaleString() + | |
" ops/frame" | |
); | |
} | |
}) | |
.run({ async: true }); | |
// typed arrays 6,7M ops/frame | |
// typed arrays mixed with arrays 12,9M ops/frame | |
// arrays 56,3M ops/frame | |
const suite2 = new Benchmark.Suite(); | |
suite2 | |
.add("typed array creation", function () { | |
const t = new Uint16Array(6); | |
t[0] = 4; | |
t[1] = 2; | |
t[2] = 0; | |
}) | |
.add("typed array creation from an array using from", function () { | |
const t = Uint16Array.from([4, 2, 0]); | |
}) | |
.add("typed array creation from an array", function () { | |
const t = new Uint16Array([4, 2, 0]); | |
}) | |
.add("regular array creation", function () { | |
const t = [4, 2, 0]; | |
}) | |
.on("complete", function () { | |
for (var i = 0; i < this.length; i++) { | |
console.log( | |
this[i].name + " " + (this[i].hz / 60).toLocaleString() + " ops/frame" | |
); | |
} | |
}) | |
.run({ async: true }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment