Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions scripts/perf.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ async function runBenchmarks() {
time: 1000,
});

// Test data for TreeInterpreter benchmarks
const simpleData = { foo: { bar: 'baz' } };
const arrayData = { items: Array.from({ length: 100 }, (_, i) => ({ id: i, name: `item${i}`, price: i * 10 })) };
const nestedData = { level1: { level2: { level3: { level4: { value: 42 } } } } };

// Baseline parsing benchmarks
bench
.add('Parser#single_expr', () => {
Expand Down Expand Up @@ -44,6 +49,41 @@ async function runBenchmarks() {
})
.add('Lexer#function_calls', () => {
jmespath.compile('sort_by(items, &price).name');
})
// TreeInterpreter evaluation benchmarks
.add('Eval#simple_field', () => {
jmespath.search(simpleData, 'foo.bar');
})
.add('Eval#array_projection', () => {
jmespath.search(arrayData, 'items[*].name');
})
.add('Eval#filter_projection', () => {
jmespath.search(arrayData, 'items[?price > `500`].name');
})
.add('Eval#function_call', () => {
jmespath.search(arrayData, 'length(items)');
})
.add('Eval#nested_access', () => {
jmespath.search(nestedData, 'level1.level2.level3.level4.value');
})
.add('Eval#slice_operation', () => {
jmespath.search(arrayData, 'items[10:20]');
})
// Runtime function call benchmarks
.add('Runtime#length_function', () => {
jmespath.search(arrayData, 'length(items)');
})
.add('Runtime#max_function', () => {
jmespath.search(arrayData, 'max(items[*].price)');
})
.add('Runtime#sort_by_function', () => {
jmespath.search(arrayData, 'sort_by(items, &price)');
})
.add('Runtime#map_function', () => {
jmespath.search(arrayData, 'map(&name, items)');
})
.add('Runtime#contains_function', () => {
jmespath.search(arrayData, 'contains(items[*].name, `"item50"`)');
});

await bench.run();
Expand Down
40 changes: 21 additions & 19 deletions src/Runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,26 +201,28 @@ export class Runtime {
return false;
}
private getTypeName(obj: JSONValue | ExpressionNode): InputArgument | undefined {
switch (Object.prototype.toString.call(obj)) {
case '[object String]':
return InputArgument.TYPE_STRING;
case '[object Number]':
return InputArgument.TYPE_NUMBER;
case '[object Array]':
return InputArgument.TYPE_ARRAY;
case '[object Boolean]':
return InputArgument.TYPE_BOOLEAN;
case '[object Null]':
return InputArgument.TYPE_NULL;
case '[object Object]':
if ((obj as ObjectDict).expref) {
return InputArgument.TYPE_EXPREF;
}
return InputArgument.TYPE_OBJECT;

default:
return;
if (obj === null) {
return InputArgument.TYPE_NULL;
}
if (typeof obj === 'string') {
return InputArgument.TYPE_STRING;
}
if (typeof obj === 'number') {
return InputArgument.TYPE_NUMBER;
}
if (typeof obj === 'boolean') {
return InputArgument.TYPE_BOOLEAN;
}
if (Array.isArray(obj)) {
return InputArgument.TYPE_ARRAY;
}
if (typeof obj === 'object') {
if ((obj as ObjectDict).expref) {
return InputArgument.TYPE_EXPREF;
}
return InputArgument.TYPE_OBJECT;
}
return;
}

createKeyFunction(exprefNode: ExpressionNode, allowedTypes: InputArgument[]): (x: JSONValue) => JSONValue {
Expand Down