aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2017-12-14 14:55:32 +1100
committerdaurnimator <quae@daurnimator.com>2017-12-14 14:56:32 +1100
commit6099ba186c38c854332ec483edd6cbca3cf94871 (patch)
treef2ac5dd59230ee285b9177be23ba312ba0e382ad
parent431d44fbc7a05dc282641172bd2ddbaf0d6cd9cc (diff)
downloadfengari-6099ba186c38c854332ec483edd6cbca3cf94871.tar.gz
fengari-6099ba186c38c854332ec483edd6cbca3cf94871.tar.bz2
fengari-6099ba186c38c854332ec483edd6cbca3cf94871.zip
src/lstrlib.js: Optimise array_cmp
Using .indexOf seems to benchmark faster than iterating
-rw-r--r--src/lstrlib.js15
1 files changed, 12 insertions, 3 deletions
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) {