From b34f3613f593f351680125db83270d740e28b900 Mon Sep 17 00:00:00 2001 From: zanxz-lol Date: Sat, 20 Dec 2025 21:30:00 -0800 Subject: [PATCH 1/3] Made listIndex and listIndexSlow faster --- src/compiler/jsexecute.js | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/compiler/jsexecute.js b/src/compiler/jsexecute.js index 61d69cd3cc4..d9b29eb6ca5 100644 --- a/src/compiler/jsexecute.js +++ b/src/compiler/jsexecute.js @@ -338,7 +338,7 @@ runtimeFunctions.compareLessThan = `const compareLessThanSlow = (v1, v2) => { } return n1 < n2; }; -const compareLessThan = (v1, v2) => typeof v1 === 'number' && typeof v2 === 'number' && !Number.isNaN(v2) ? v1 < v2 : compareLessThanSlow(v1, v2)`; +const compareLessThan = (v1, v2) => typeof v1 === 'number' && typeof v2 === 'number' && !Number.isNaN(v2) ? v1 < v2 : compareLessThanSlow(v1, v2);`; /** * Generate a random integer. @@ -411,26 +411,25 @@ runtimeFunctions.distance = `const distance = menu => { * @returns {number} 0 based list index, or -1 if invalid. */ baseRuntime += `const listIndexSlow = (index, length) => { - if (index === 'last') { - return length - 1; - } else if (index === 'random' || index === 'any') { - if (length > 0) { - return (Math.random() * length) | 0; - } - return -1; - } - index = (+index || 0) | 0; - if (index < 1 || index > length) { - return -1; - } - return index - 1; + switch(index) { + case 'last': + return length - 1; + case 'any': + case 'random': + return (length > 0) ? ((Math.random() * length) | 0) : -1; + default: + index = (+index || 0) | 0; + return (index < 1 || index > length) ? -1: index - 1; + } }; + const listIndex = (index, length) => { - if (typeof index !== 'number') { - return listIndexSlow(index, length); + /* expect best case since 'random', 'last', and 'any' are never usually used by people */ + if (typeof index === 'number') { + index = index | 0; + return index < 1 || index > length ? -1 : index - 1; } - index = index | 0; - return index < 1 || index > length ? -1 : index - 1; + return listIndexSlow(index, length); };`; /** From 022e10e8a41e53e80b27b37af3ea6b515ac46332 Mon Sep 17 00:00:00 2001 From: zanxz-lol Date: Sat, 20 Dec 2025 22:08:54 -0800 Subject: [PATCH 2/3] Made listContains faster by removing indexOf() --- src/compiler/jsexecute.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/compiler/jsexecute.js b/src/compiler/jsexecute.js index d9b29eb6ca5..a75a5b0647f 100644 --- a/src/compiler/jsexecute.js +++ b/src/compiler/jsexecute.js @@ -424,7 +424,7 @@ baseRuntime += `const listIndexSlow = (index, length) => { }; const listIndex = (index, length) => { - /* expect best case since 'random', 'last', and 'any' are never usually used by people */ + /* expect a number instead since barely anyone uses a string */ if (typeof index === 'number') { index = index | 0; return index < 1 || index > length ? -1 : index - 1; @@ -501,10 +501,7 @@ runtimeFunctions.listDelete = `const listDelete = (list, idx) => { * @returns {boolean} True if the list contains the item */ runtimeFunctions.listContains = `const listContains = (list, item) => { - // TODO: evaluate whether indexOf is worthwhile here - if (list.value.indexOf(item) !== -1) { - return true; - } + /* using indexOf then checking using compareEqual is terrible for performance. indexOf isn't reliable anyways */ for (let i = 0; i < list.value.length; i++) { if (compareEqual(list.value[i], item)) { return true; From 40b5eabc94097c67f68cbf022ae7d05ce607766f Mon Sep 17 00:00:00 2001 From: zanxz-lol Date: Sun, 21 Dec 2025 11:40:15 -0800 Subject: [PATCH 3/3] Reverted changes to listContains() --- src/compiler/jsexecute.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/compiler/jsexecute.js b/src/compiler/jsexecute.js index a75a5b0647f..86851471d11 100644 --- a/src/compiler/jsexecute.js +++ b/src/compiler/jsexecute.js @@ -501,7 +501,10 @@ runtimeFunctions.listDelete = `const listDelete = (list, idx) => { * @returns {boolean} True if the list contains the item */ runtimeFunctions.listContains = `const listContains = (list, item) => { - /* using indexOf then checking using compareEqual is terrible for performance. indexOf isn't reliable anyways */ + if (list.value.indexOf(item) !== -1) { + return true; + } + /* i stand corrected */ for (let i = 0; i < list.value.length; i++) { if (compareEqual(list.value[i], item)) { return true;