🚀 benchmark

This commit is contained in:
platane
2020-07-21 18:15:41 +02:00
parent 1898ec16e4
commit e637604df1
13 changed files with 182 additions and 61 deletions

View File

@@ -1,9 +1,7 @@
import { Grid, Color } from "./grid";
import { Grid, Color, setColor, createEmptyGrid } from "./grid";
const rand = (a: number, b: number) => Math.floor(Math.random() * (b - a)) + a;
export const generateEmptyGrid = (width: number, height: number) =>
generateRandomGrid(width, height, { colors: [], emptyP: 1 });
const defaultRand = (a: number, b: number) =>
Math.floor(Math.random() * (b - a)) + a;
export const generateRandomGrid = (
width: number,
@@ -11,17 +9,17 @@ export const generateRandomGrid = (
options: { colors: Color[]; emptyP: number } = {
colors: [1, 2, 3],
emptyP: 2,
}
},
rand = defaultRand
): Grid => {
const g = {
width,
height,
data: Array.from({ length: width * height }, () => {
const x = rand(-options.emptyP, options.colors.length);
const grid = createEmptyGrid(width, height);
return x < 0 ? null : options.colors[x];
}),
};
for (let x = width; x--; )
for (let y = height; y--; ) {
const k = rand(-options.emptyP, options.colors.length);
return g;
if (k >= 0) setColor(grid, x, y, options.colors[k]);
}
return grid;
};