mirror of
https://github.com/fluencelabs/jsonpath
synced 2025-06-18 02:21:30 +00:00
serde_json::Value clone 제거
This commit is contained in:
@ -1,118 +1,124 @@
|
||||
let json = {
|
||||
"store": {
|
||||
"book": [
|
||||
'store': {
|
||||
'book': [
|
||||
{
|
||||
"category": "reference",
|
||||
"author": "Nigel Rees",
|
||||
"title": "Sayings of the Century",
|
||||
"price": 8.95
|
||||
'category': 'reference',
|
||||
'author': 'Nigel Rees',
|
||||
'title': 'Sayings of the Century',
|
||||
'price': 8.95,
|
||||
},
|
||||
{
|
||||
"category": "fiction",
|
||||
"author": "Evelyn Waugh",
|
||||
"title": "Sword of Honour",
|
||||
"price": 12.99
|
||||
'category': 'fiction',
|
||||
'author': 'Evelyn Waugh',
|
||||
'title': 'Sword of Honour',
|
||||
'price': 12.99,
|
||||
},
|
||||
{
|
||||
"category": "fiction",
|
||||
"author": "Herman Melville",
|
||||
"title": "Moby Dick",
|
||||
"isbn": "0-553-21311-3",
|
||||
"price": 8.99
|
||||
'category': 'fiction',
|
||||
'author': 'Herman Melville',
|
||||
'title': 'Moby Dick',
|
||||
'isbn': '0-553-21311-3',
|
||||
'price': 8.99,
|
||||
},
|
||||
{
|
||||
"category": "fiction",
|
||||
"author": "J. R. R. Tolkien",
|
||||
"title": "The Lord of the Rings",
|
||||
"isbn": "0-395-19395-8",
|
||||
"price": 22.99
|
||||
}
|
||||
'category': 'fiction',
|
||||
'author': 'J. R. R. Tolkien',
|
||||
'title': 'The Lord of the Rings',
|
||||
'isbn': '0-395-19395-8',
|
||||
'price': 22.99,
|
||||
},
|
||||
],
|
||||
"bicycle": {
|
||||
"color": "red",
|
||||
"price": 19.95
|
||||
}
|
||||
'bicycle': {
|
||||
'color': 'red',
|
||||
'price': 19.95,
|
||||
},
|
||||
},
|
||||
"expensive": 10
|
||||
'expensive': 10,
|
||||
};
|
||||
|
||||
const jp = require('jsonpath');
|
||||
const jpw = require('@nodejs/jsonpath-wasm');
|
||||
const Benchmark = require('benchmark');
|
||||
const iter = 100000;
|
||||
|
||||
function compareJsonpath(path) {
|
||||
let r1 = jp.query(json, path);
|
||||
let r2 = jpw.read(json, path);
|
||||
|
||||
let template = jpw.compile(path);
|
||||
|
||||
var suite = new Benchmark.Suite;
|
||||
|
||||
suite.add('jp', function() {
|
||||
jp.query(json, path);
|
||||
})
|
||||
.add('jpw', function() {
|
||||
template(json);
|
||||
})
|
||||
.on('cycle', function(event) {
|
||||
console.log(String(event.target));
|
||||
})
|
||||
.on('complete', function() {
|
||||
console.log('Fastest is ' + this.filter('fastest').map('name'));
|
||||
console.log('Slowest is ' + this.filter('slowest').map('name'));
|
||||
})
|
||||
.run({ 'async': true });
|
||||
}
|
||||
|
||||
function compareEmptyFunction() {
|
||||
var suite = new Benchmark.Suite;
|
||||
|
||||
suite.add('js', function() {
|
||||
})
|
||||
.add('rust', function() {
|
||||
jpw.testa();
|
||||
})
|
||||
.on('cycle', function(event) {
|
||||
console.log(String(event.target));
|
||||
})
|
||||
.on('complete', function() {
|
||||
console.log('Fastest is ' + this.filter('fastest').map('name'));
|
||||
console.log('Slowest is ' + this.filter('slowest').map('name'));
|
||||
})
|
||||
.run({});
|
||||
}
|
||||
|
||||
function jsonpathOnly() {
|
||||
for(var i = 0; i < 100000 ; i++) {
|
||||
function jsonpath() {
|
||||
for (var i = 0; i < iter; i++) {
|
||||
let _ = jp.query(json, '$..book[?(@.price<30 && @.category=="fiction")]');
|
||||
}
|
||||
}
|
||||
|
||||
function jsonpathWasmOnly() {
|
||||
let reader = jpw.reader(json);
|
||||
for(var i = 0; i < 100000 ; i++) {
|
||||
let _ = reader('$..book[?(@.price<30 && @.category=="fiction")]');
|
||||
function wasmSelector() {
|
||||
let selector = jpw.selector(json);
|
||||
for (var i = 0; i < iter; i++) {
|
||||
let _ = selector('$..book[?(@.price<30 && @.category=="fiction")]');
|
||||
}
|
||||
}
|
||||
|
||||
if(process.argv.length < 3) {
|
||||
let functions = ['', 'compareJsonpath', 'compareEmptyFunction', 'jsonpathOnly', 'jsonpathWasmOnly'];
|
||||
console.log("node bench.js", functions.join("\n\t|"));
|
||||
return;
|
||||
function wasmCompile() {
|
||||
let template = jpw.compile('$..book[?(@.price<30 && @.category=="fiction")]');
|
||||
for (var i = 0; i < iter; i++) {
|
||||
let _ = template(json);
|
||||
}
|
||||
}
|
||||
|
||||
function wasmCompileAlloc() {
|
||||
let ptr = jpw.alloc_json(json);
|
||||
if (ptr == 0) {
|
||||
console.error('Invalid pointer');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
let template = jpw.compile('$..book[?(@.price<30 && @.category=="fiction")]');
|
||||
for (var i = 0; i < iter; i++) {
|
||||
let _ = template(ptr);
|
||||
}
|
||||
} finally {
|
||||
jpw.dealloc_json(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
function wasmSelect() {
|
||||
for (var i = 0; i < iter; i++) {
|
||||
let _ = jpw.select(json, '$..book[?(@.price<30 && @.category=="fiction")]');
|
||||
}
|
||||
}
|
||||
|
||||
function wasmSelectAlloc() {
|
||||
let ptr = jpw.alloc_json(json);
|
||||
if (ptr == 0) {
|
||||
console.error('Invalid pointer');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
for (var i = 0; i < iter; i++) {
|
||||
let _ = jpw.select(ptr, '$..book[?(@.price<30 && @.category=="fiction")]');
|
||||
}
|
||||
} finally {
|
||||
jpw.dealloc_json(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
let functionName = process.argv[2];
|
||||
|
||||
switch (functionName) {
|
||||
case 'compareJsonpath':
|
||||
compareJsonpath('$..book[?(@.price<30 && @.category=="fiction")]');
|
||||
case 'jsonpath':
|
||||
jsonpath();
|
||||
break;
|
||||
case 'compareEmptyFunction':
|
||||
compareEmptyFunction();
|
||||
case 'wasmSelector':
|
||||
wasmSelector();
|
||||
break;
|
||||
case 'jsonpathWasmOnly':
|
||||
jsonpathWasmOnly();
|
||||
case 'wasmCompile':
|
||||
wasmCompile();
|
||||
break;
|
||||
case 'wasmSelect':
|
||||
wasmSelect();
|
||||
break;
|
||||
case 'wasmCompileAlloc':
|
||||
wasmCompileAlloc();
|
||||
break;
|
||||
case 'wasmSelectAlloc':
|
||||
wasmSelectAlloc();
|
||||
default:
|
||||
jsonpathOnly();
|
||||
}
|
||||
console.error('Invalid function name');
|
||||
}
|
||||
|
Reference in New Issue
Block a user