From 807f0feec15473e06a69aa0f82dbc27b7223959a Mon Sep 17 00:00:00 2001 From: valadaptive Date: Fri, 14 Jun 2024 17:46:30 -0400 Subject: [PATCH] Optimize simple scalar inputs in evaluateFast --- src/interpreter/block-context.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/interpreter/block-context.ts b/src/interpreter/block-context.ts index 05d5cc6..48520ba 100644 --- a/src/interpreter/block-context.ts +++ b/src/interpreter/block-context.ts @@ -41,12 +41,12 @@ export default class BlockContext { } *evaluate(input: Block | Block[] | string | number | boolean): BlockGenerator { - if (Array.isArray(input)) { - return yield* this.thread.evaluateScript(input); - } if (typeof input === 'string' || typeof input === 'number' || typeof input === 'boolean') { return input; } + if (Array.isArray(input)) { + return yield* this.thread.evaluateScript(input); + } return yield* this.thread.evaluateBlock(input); } @@ -57,6 +57,10 @@ export default class BlockContext { * @param input The input to evaluate. */ evaluateFast(input: Block | string | number | boolean): string | number | boolean { + // Fast path for scalar inputs; inlined to avoid a function call + if (typeof input === 'string' || typeof input === 'number' || typeof input === 'boolean') { + return input; + } const generator = this.evaluate(input); const {done, value} = generator.next(); if (done) {