From 79ff8a0d170bc0e54ef6ce16f08267560fbf1180 Mon Sep 17 00:00:00 2001 From: Jamie Bray Date: Tue, 29 Mar 2022 00:07:51 +0800 Subject: [PATCH] 9.7s Do not create a new ImageData object every render --- src/graphics/loop.ts | 4 +++- src/graphics/render.ts | 3 +-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/graphics/loop.ts b/src/graphics/loop.ts index 9c83c8d..79bd130 100644 --- a/src/graphics/loop.ts +++ b/src/graphics/loop.ts @@ -11,6 +11,7 @@ export type Meta = { generationsAndMax: Uint32Array; // [computations, maxGenerations] renders: number; rendersMinimumMilliseconds: number; + imageData: ImageData; onDone: (meta: Meta) => void; }; @@ -34,6 +35,7 @@ export const setup = async ( generationsAndMax, rendersMinimumMilliseconds, onDone, + imageData: new ImageData(viewWidth + 2, viewHeight + 2), renders: 0, }; @@ -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; diff --git a/src/graphics/render.ts b/src/graphics/render.ts index de4d4ac..22f3ada 100644 --- a/src/graphics/render.ts +++ b/src/graphics/render.ts @@ -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;