From 580fe4d7183fc7771b3bad638085b912fb98266a Mon Sep 17 00:00:00 2001 From: Joshua Appelman Date: Thu, 26 Jun 2014 00:15:15 +0200 Subject: [PATCH] Replaces Array.prototype.reduce with a for loop. As this is possibly the hottest code path, this speeds up average total execution time with about 25%, benchmarking shows. --- index.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index fa744fc..6614c21 100644 --- a/index.js +++ b/index.js @@ -34,13 +34,17 @@ function applyStyle() { return str; } - return applyStyle._styles.reduce(function (str, name) { - var code = ansiStyles[name]; + var nestedStyles = applyStyle._styles; + + for (var i = 0; i < nestedStyles.length; i++) { + var code = ansiStyles[nestedStyles[i]]; // Replace any instances already present with a re-opening code // otherwise only the part of the string until said closing code // will be colored, and the rest will simply be 'plain'. - return code.open + str.replace(code.closeRe, code.open) + code.close; - }, str) ; + str = code.open + str.replace(code.closeRe, code.open) + code.close; + } + + return str; } function init() {