From 144d14797751e7bf4de7d1444b4122f464a39014 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 14 Dec 2017 14:26:33 +1100 Subject: src/lstrlib.js: Optimise find_subarray using .indexOf --- src/lstrlib.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'src') 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; -- cgit v1.2.3-70-g09d2