Skip to content

Commit

Permalink
Optimize simple scalar inputs in evaluateFast
Browse files Browse the repository at this point in the history
  • Loading branch information
valadaptive committed Jun 14, 2024
1 parent ad38a51 commit 807f0fe
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/interpreter/block-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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) {
Expand Down

0 comments on commit 807f0fe

Please sign in to comment.