aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2017-12-14 14:26:33 +1100
committerdaurnimator <quae@daurnimator.com>2017-12-14 14:40:54 +1100
commit144d14797751e7bf4de7d1444b4122f464a39014 (patch)
tree44ef44cf3b1045e4622a43e34c254ce9db034706
parent3e7c102eefbaae9e6bc839b11bba79aee1c5e040 (diff)
downloadfengari-144d14797751e7bf4de7d1444b4122f464a39014.tar.gz
fengari-144d14797751e7bf4de7d1444b4122f464a39014.tar.bz2
fengari-144d14797751e7bf4de7d1444b4122f464a39014.zip
src/lstrlib.js: Optimise find_subarray using .indexOf
-rw-r--r--src/lstrlib.js17
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;