From 04bbe30a000549f54b03cb9083e7ecdfad7e2e04 Mon Sep 17 00:00:00 2001 From: David Braun Date: Tue, 17 Oct 2017 10:32:19 -0400 Subject: [PATCH] slice: Refactor for improved performance. --- lib/index.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 04d0792..210f1fd 100644 --- a/lib/index.js +++ b/lib/index.js @@ -203,12 +203,20 @@ const Channel = function(bufferLength = 0) { slice: (start, end = Infinity) => { const output = Channel(); (async () => { - for (let index = 0; index < end; index++) { + for (let index = 0; index < start; index++) { const value = await readOnly.shift(); if (value === undefined) { break; - } else if (index >= start) { + } + } + + for (let index = start; index < end; index++) { + const value = await readOnly.shift(); + + if (value === undefined) { + break; + } else { await output.push(value); } }