diff options
author | daurnimator <quae@daurnimator.com> | 2017-12-14 14:26:33 +1100 |
---|---|---|
committer | daurnimator <quae@daurnimator.com> | 2017-12-14 14:40:54 +1100 |
commit | 144d14797751e7bf4de7d1444b4122f464a39014 (patch) | |
tree | 44ef44cf3b1045e4622a43e34c254ce9db034706 /src/lstrlib.js | |
parent | 3e7c102eefbaae9e6bc839b11bba79aee1c5e040 (diff) | |
download | fengari-144d14797751e7bf4de7d1444b4122f464a39014.tar.gz fengari-144d14797751e7bf4de7d1444b4122f464a39014.tar.bz2 fengari-144d14797751e7bf4de7d1444b4122f464a39014.zip |
src/lstrlib.js: Optimise find_subarray using .indexOf
Diffstat (limited to 'src/lstrlib.js')
-rw-r--r-- | src/lstrlib.js | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/src/lstrlib.js b/src/lstrlib.js index c4dd1ae..23b0404 100644 --- a/src/lstrlib.js +++ b/src/lstrlib.js @@ -1148,13 +1148,20 @@ const reprepstate = function(ms) { const find_subarray = function(arr, subarr, from_index) { var i = from_index >>> 0, - sl = subarr.length, - l = arr.length + 1 - sl; + sl = subarr.length; - loop: for (; i < l; i++) { - for (let j = 0; j < sl; j++) - if (arr[i+j] !== subarr[j]) + if (sl === 0) + return i; + + loop: for (;;) { + i = arr.indexOf(subarr[0], i); + if (i === -1) break; + for (let j = 1; j < sl; j++) { + if (arr[i+j] !== subarr[j]) { + i++; continue loop; + } + } return i; } return -1; |