From 6099ba186c38c854332ec483edd6cbca3cf94871 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 14 Dec 2017 14:55:32 +1100 Subject: src/lstrlib.js: Optimise array_cmp Using .indexOf seems to benchmark faster than iterating --- src/lstrlib.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/lstrlib.js b/src/lstrlib.js index 0572e95..362942d 100644 --- a/src/lstrlib.js +++ b/src/lstrlib.js @@ -965,12 +965,21 @@ const end_capture = function(ms, s, p) { /* Compare the elements of arrays 'a' and 'b' to see if they contain the same elements */ const array_cmp = function(a, ai, b, bi, len) { + if (len === 0) + return true; let aj = ai+len; - for (; ai < aj; ai++, bi++) { - if (a[ai] !== b[bi]) + loop: for (;;) { + ai = a.indexOf(b[bi], ai); + if (ai === -1 || ai >= aj) return false; + for (let j = 1; j < len; j++) { + if (a[ai+j] !== b[bi+j]) { + ai++; + continue loop; + } + } + return true; } - return true; }; const match_capture = function(ms, s, l) { -- cgit v1.2.3-54-g00ecf