From 6d82c0f115713ae729dfadc024ddf6d70e21b25f Mon Sep 17 00:00:00 2001 From: "Michael \"Z\" Goddard" Date: Fri, 3 Nov 2017 16:24:12 -0400 Subject: [PATCH] In place filter threads at end of stepThreads Skip a large array allocation by filtering done threads in place with a for loop. --- src/engine/sequencer.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/engine/sequencer.js b/src/engine/sequencer.js index 9664290c4..f4edc966c 100644 --- a/src/engine/sequencer.js +++ b/src/engine/sequencer.js @@ -84,13 +84,28 @@ class Sequencer { ranFirstTick = true; } // Filter inactive threads from `this.runtime.threads`. - this.runtime.threads = this.runtime.threads.filter((thread, i) => { + numActiveThreads = 0; + for (let i = 0; i < this.runtime.threads.length; i++) { + const thread = this.runtime.threads[i]; if (doneThreads[i] === null) { - return false; + this.runtime.threads[numActiveThreads] = thread; + numActiveThreads++; } - return true; - }); - return doneThreads.filter(Boolean); + } + this.runtime.threads.length = numActiveThreads; + + // Filter undefined and null values from `doneThreads`. + let numDoneThreads = 0; + for (let i = 0; i < doneThreads.length; i++) { + const maybeThread = doneThreads[i]; + if (maybeThread !== null) { + doneThreads[numDoneThreads] = maybeThread; + numDoneThreads++; + } + } + doneThreads.length = numDoneThreads; + + return doneThreads; } /**