Skip to content

Commit

Permalink
9.7s Do not create a new ImageData object every render
Browse files Browse the repository at this point in the history
  • Loading branch information
Jumbub committed Mar 28, 2022
1 parent 8d4d13b commit 79ff8a0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/graphics/loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type Meta = {
generationsAndMax: Uint32Array; // [computations, maxGenerations]
renders: number;
rendersMinimumMilliseconds: number;
imageData: ImageData;
onDone: (meta: Meta) => void;
};

Expand All @@ -34,6 +35,7 @@ export const setup = async (
generationsAndMax,
rendersMinimumMilliseconds,
onDone,
imageData: new ImageData(viewWidth + 2, viewHeight + 2),
renders: 0,
};

Expand All @@ -51,7 +53,7 @@ export const setup = async (
};

export const run = (meta: Meta) => {
const renderLambda = () => render(meta.board, meta.context);
const renderLambda = () => render(meta.imageData, meta.board, meta.context);

const interval = setInterval(() => {
const [generations, maxGenerations] = meta.generationsAndMax;
Expand Down
3 changes: 1 addition & 2 deletions src/graphics/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ export const DEAD_COLOR_32 = 16777215;

const COLOR_DIFF = ALIVE_COLOR_32 - DEAD_COLOR_32;

export const render = (board: Board, context: CanvasRenderingContext2D) => {
export const render = (image: ImageData, board: Board, context: CanvasRenderingContext2D) => {
const { width, height } = board;

const image = new ImageData(width, height);
const image32 = new Uint32Array(image.data.buffer);
const size = width * height;

Expand Down

0 comments on commit 79ff8a0

Please sign in to comment.