138 lines
3.2 KiB
JavaScript
Raw Normal View History

2019-03-07 18:44:06 +09:00
let json = {
2019-03-11 17:35:15 +09:00
'store': {
'book': [
2019-03-07 18:44:06 +09:00
{
2019-03-11 17:35:15 +09:00
'category': 'reference',
'author': 'Nigel Rees',
'title': 'Sayings of the Century',
'price': 8.95,
2019-03-07 18:44:06 +09:00
},
{
2019-03-11 17:35:15 +09:00
'category': 'fiction',
'author': 'Evelyn Waugh',
'title': 'Sword of Honour',
'price': 12.99,
2019-03-07 18:44:06 +09:00
},
{
2019-03-11 17:35:15 +09:00
'category': 'fiction',
'author': 'Herman Melville',
'title': 'Moby Dick',
'isbn': '0-553-21311-3',
'price': 8.99,
2019-03-07 18:44:06 +09:00
},
{
2019-03-11 17:35:15 +09:00
'category': 'fiction',
'author': 'J. R. R. Tolkien',
'title': 'The Lord of the Rings',
'isbn': '0-395-19395-8',
'price': 22.99,
},
2019-03-07 18:44:06 +09:00
],
2019-03-11 17:35:15 +09:00
'bicycle': {
'color': 'red',
'price': 19.95,
},
2019-03-07 18:44:06 +09:00
},
2019-03-11 17:35:15 +09:00
'expensive': 10,
2019-03-07 18:44:06 +09:00
};
2019-03-14 22:30:42 +09:00
let jsonStr = JSON.stringify(json);
2019-03-07 18:44:06 +09:00
2019-03-14 22:30:42 +09:00
function getJson() {
return JSON.parse(jsonStr);
}
const path = '$..book[?(@.price<30 && @.category=="fiction")]';
2019-03-07 18:44:06 +09:00
const jp = require('jsonpath');
2019-03-08 17:27:24 +09:00
const jpw = require('@nodejs/jsonpath-wasm');
2019-03-14 22:30:42 +09:00
const jpwRs = require('jsonpath-rs');
2019-03-07 18:44:06 +09:00
2019-03-11 17:35:15 +09:00
function jsonpath() {
for (var i = 0; i < iter; i++) {
2019-03-14 22:30:42 +09:00
let _ = jp.query(getJson(), path);
}
}
function nativeCompile() {
let template = jpwRs.compile(path);
for (var i = 0; i < iter; i++) {
let _ = template(JSON.stringify(json));
}
}
function nativeSelector() {
let selector = jpwRs.selector(getJson());
for (var i = 0; i < iter; i++) {
let _ = selector(path);
}
}
function nativeSelect() {
for (var i = 0; i < iter; i++) {
let _ = jpwRs.select(JSON.stringify(json), path);
2019-03-11 17:35:15 +09:00
}
}
2019-03-07 18:44:06 +09:00
2019-03-11 17:35:15 +09:00
function wasmSelector() {
2019-03-14 22:30:42 +09:00
let selector = jpw.selector(getJson());
2019-03-11 17:35:15 +09:00
for (var i = 0; i < iter; i++) {
2019-03-14 22:30:42 +09:00
let _ = selector(path);
2019-03-11 17:35:15 +09:00
}
}
2019-03-07 18:44:06 +09:00
2019-03-11 17:35:15 +09:00
function wasmCompile() {
2019-03-14 22:30:42 +09:00
let template = jpw.compile(path);
2019-03-11 17:35:15 +09:00
for (var i = 0; i < iter; i++) {
2019-03-14 22:30:42 +09:00
let _ = template(getJson());
2019-03-11 17:35:15 +09:00
}
2019-03-07 18:44:06 +09:00
}
2019-03-11 17:35:15 +09:00
function wasmCompileAlloc() {
let ptr = jpw.allocJson(getJson());
2019-03-11 17:35:15 +09:00
if (ptr == 0) {
console.error('Invalid pointer');
return;
}
2019-03-07 18:44:06 +09:00
2019-03-11 17:35:15 +09:00
try {
2019-03-14 22:30:42 +09:00
let template = jpw.compile(path);
2019-03-11 17:35:15 +09:00
for (var i = 0; i < iter; i++) {
let _ = template(ptr);
}
} finally {
jpw.deallocJson(ptr);
2019-03-11 17:35:15 +09:00
}
2019-03-07 18:44:06 +09:00
}
2019-03-11 17:35:15 +09:00
function wasmSelect() {
for (var i = 0; i < iter; i++) {
2019-03-14 22:30:42 +09:00
let _ = jpw.select(getJson(), path);
2019-03-08 17:27:24 +09:00
}
}
2019-03-11 17:35:15 +09:00
function wasmSelectAlloc() {
let ptr = jpw.allocJson(getJson());
2019-03-11 17:35:15 +09:00
if (ptr == 0) {
console.error('Invalid pointer');
return;
2019-03-07 18:44:06 +09:00
}
2019-03-11 17:35:15 +09:00
try {
for (var i = 0; i < iter; i++) {
2019-03-14 22:30:42 +09:00
let _ = jpw.select(ptr, path);
2019-03-11 17:35:15 +09:00
}
} finally {
jpw.deallocJson(ptr);
}
}
function wasmSelectorClass() {
let selector = new jpw.Selector();
for (var i = 0; i < iter; i++) {
selector.path(path);
selector.value(jsonStr);
let _ = selector.selectToStr();
2019-03-11 17:35:15 +09:00
}
2019-03-07 18:44:06 +09:00
}
2019-03-14 22:30:42 +09:00
const functionName = process.argv[2];
const iter = parseInt(process.argv[3], 10);
eval(functionName + "()");