Last active
January 22, 2020 19:11
-
-
Save DinoSourcesRex/72ebc9a881878254901c610fc0854747 to your computer and use it in GitHub Desktop.
Test for navigating a json object graph
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { performance } = require("perf_hooks"); | |
const _ = require("lodash"); | |
const http = require("http"); | |
const jp = require("jsonpath"); | |
const hostname = "127.0.0.1"; | |
const port = 3000; | |
const server = http.createServer((req, res) => {}); | |
const totalExecutions = 1000000; | |
// const totalExecutions = 1; | |
server.listen(port, hostname, () => { | |
console.log(`Server running at http://${hostname}:${port}/`); | |
executeTest("empty test", function() {}); | |
executeTest("split and reduce", (data, query) => { | |
return jsonPath(query, data); | |
}); | |
executeTest("lodash", (data, query) => { | |
return _.property(query)(data); | |
}); | |
executeTest("json path", (data, query) => { | |
return jp.query(data, query); | |
}); | |
}); | |
function jsonPath(path, obj) { | |
return path.split(".").reduce((o, p) => o[p], obj); | |
} | |
function executeTest(testName, callback) { | |
console.log(`Executing test: ${testName}`); | |
var data = getData(); | |
var t0 = performance.now(); | |
for (var i = 0; i < totalExecutions; i++) { | |
const queries = [ | |
]; | |
queries.forEach(query => { | |
const result = callback(data, query); | |
// console.log(query, result); | |
}); | |
} | |
var t1 = performance.now(); | |
var finalTime = t1 - t0; | |
console.log( | |
`Average time for ${totalExecutions.toLocaleString()} executions: ${finalTime} milliseconds.\n` | |
); | |
} | |
function getData() { | |
return { }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment