From dc9d1c4ee675bfd514bc53b27d0e0d846dc6c157 Mon Sep 17 00:00:00 2001 From: Andy Edwards Date: Tue, 3 Dec 2019 10:47:05 -0600 Subject: [PATCH] fix: use switch (num) to cover more common cases MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Just an idea, I don't know if there are any ramifications to this, but according to [my jsperf test](https://jsperf.com/repeat-stream-optimization) this is 58% faster for `num <= 5` 👍 --- index.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 4459afd..f2fd596 100644 --- a/index.js +++ b/index.js @@ -44,8 +44,14 @@ function repeat(str, num) { } // cover common, quick use cases - if (num === 1) return str; - if (num === 2) return str + str; + switch (num) { + case 0: return ''; + case 1: return str; + case 2: return str + str; + case 3: return str + str + str; + case 4: return str + str + str + str; + case 5: return str + str + str + str + str; + } var max = str.length * num; if (cache !== str || typeof cache === 'undefined') {