perf: implement function cache and optimize translation for better rendering performance
This commit is contained in:
parent
d4211fc2d3
commit
8daff2b564
13
dist/state.js
vendored
13
dist/state.js
vendored
@ -86,13 +86,18 @@ let _disableRunCodeError = false;
|
|||||||
function setDisableRunCodeError(value) {
|
function setDisableRunCodeError(value) {
|
||||||
_disableRunCodeError = value;
|
_disableRunCodeError = value;
|
||||||
}
|
}
|
||||||
|
const _fnCache = /* @__PURE__ */ new Map();
|
||||||
function _runCode(code, vars, thisObj, extendVars) {
|
function _runCode(code, vars, thisObj, extendVars) {
|
||||||
const argKeys = [...Object.keys(extendVars || {}), ...Object.keys(vars || {})];
|
const argKeys = [...Object.keys(extendVars || {}), ...Object.keys(vars || {})];
|
||||||
const argValues = [...Object.values(extendVars || {}), ...Object.values(vars || {})];
|
const argValues = [...Object.values(extendVars || {}), ...Object.values(vars || {})];
|
||||||
argKeys.push(code);
|
const cacheKey = code + argKeys.join(",");
|
||||||
try {
|
try {
|
||||||
const r = new Function(...argKeys).apply(thisObj, argValues);
|
let fn = _fnCache.get(cacheKey);
|
||||||
return r;
|
if (!fn) {
|
||||||
|
fn = new Function(...argKeys, code);
|
||||||
|
_fnCache.set(cacheKey, fn);
|
||||||
|
}
|
||||||
|
return fn.apply(thisObj, argValues);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (!_disableRunCodeError) console.error(e, extendVars, [code, extendVars, vars, thisObj]);
|
if (!_disableRunCodeError) console.error(e, extendVars, [code, extendVars, vars, thisObj]);
|
||||||
return null;
|
return null;
|
||||||
@ -110,7 +115,7 @@ let _translator = (text, args) => {
|
|||||||
};
|
};
|
||||||
const SetTranslator = (fn) => _translator = fn;
|
const SetTranslator = (fn) => _translator = fn;
|
||||||
const _translate = (text) => {
|
const _translate = (text) => {
|
||||||
if (!text || typeof text !== "string") return text;
|
if (!text || typeof text !== "string" || !text.includes("{#")) return text;
|
||||||
return text.replace(/\{#(.+?)#\}/g, (m, content) => {
|
return text.replace(/\{#(.+?)#\}/g, (m, content) => {
|
||||||
const parts = content.split("||").map((s) => s.trim());
|
const parts = content.split("||").map((s) => s.trim());
|
||||||
const rawText = parts[0];
|
const rawText = parts[0];
|
||||||
|
|||||||
2
dist/state.min.js
vendored
2
dist/state.min.js
vendored
File diff suppressed because one or more lines are too long
12
src/core.js
12
src/core.js
@ -5,13 +5,19 @@ export function setDisableRunCodeError(value) {
|
|||||||
_disableRunCodeError = value;
|
_disableRunCodeError = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const _fnCache = new Map();
|
||||||
|
|
||||||
export function _runCode(code, vars, thisObj, extendVars) {
|
export function _runCode(code, vars, thisObj, extendVars) {
|
||||||
const argKeys = [...Object.keys(extendVars || {}), ...Object.keys(vars || {})];
|
const argKeys = [...Object.keys(extendVars || {}), ...Object.keys(vars || {})];
|
||||||
const argValues = [...Object.values(extendVars || {}), ...Object.values(vars || {})];
|
const argValues = [...Object.values(extendVars || {}), ...Object.values(vars || {})];
|
||||||
argKeys.push(code);
|
const cacheKey = code + argKeys.join(',');
|
||||||
try {
|
try {
|
||||||
const r = new Function(...argKeys).apply(thisObj, argValues);
|
let fn = _fnCache.get(cacheKey);
|
||||||
return r;
|
if (!fn) {
|
||||||
|
fn = new Function(...argKeys, code);
|
||||||
|
_fnCache.set(cacheKey, fn);
|
||||||
|
}
|
||||||
|
return fn.apply(thisObj, argValues);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (!_disableRunCodeError) console.error(e, extendVars, [code, extendVars, vars, thisObj]);
|
if (!_disableRunCodeError) console.error(e, extendVars, [code, extendVars, vars, thisObj]);
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@ -13,7 +13,7 @@ let _translator = (text, args) => {
|
|||||||
export const SetTranslator = (fn) => _translator = fn;
|
export const SetTranslator = (fn) => _translator = fn;
|
||||||
|
|
||||||
const _translate = (text) => {
|
const _translate = (text) => {
|
||||||
if (!text || typeof text !== 'string') return text;
|
if (!text || typeof text !== 'string' || !text.includes('{#')) return text;
|
||||||
return text.replace(/\{#(.+?)#\}/g, (m, content) => {
|
return text.replace(/\{#(.+?)#\}/g, (m, content) => {
|
||||||
const parts = content.split('||').map(s => s.trim());
|
const parts = content.split('||').map(s => s.trim());
|
||||||
const rawText = parts[0];
|
const rawText = parts[0];
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user