mirror of
https://github.com/qntm/fastjson
synced 2026-03-14 01:24:41 +01:00
Compare commits
2 Commits
6578b5ebb4
...
d743846a53
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d743846a53 | ||
|
|
694f0f5211 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -45,3 +45,4 @@ Temporary Items
|
|||||||
# mine
|
# mine
|
||||||
coverage
|
coverage
|
||||||
node_modules
|
node_modules
|
||||||
|
package-lock.json
|
||||||
|
|||||||
@ -53,6 +53,9 @@ How this other representation should be constructed is not specified. The method
|
|||||||
|
|
||||||
Likewise, how such text should be generated from the input, or even whether any input should be accepted, is not specified. `fastjson.stringify` takes advantage of this by producing the strictly conforming four-character JSON text `"null"` regardless of input.
|
Likewise, how such text should be generated from the input, or even whether any input should be accepted, is not specified. `fastjson.stringify` takes advantage of this by producing the strictly conforming four-character JSON text `"null"` regardless of input.
|
||||||
|
|
||||||
|
## Performance
|
||||||
|
The `fastjson` functions have demostrated to be between 4,000,000 and 40,000,000 times faster than their built-in `JSON` equivalents on large amounts of data. The benchmarks are open source and [located in this repo](https://github.com/qntm/fastjson/tree/main/benchmarks/).
|
||||||
|
|
||||||
## Notes
|
## Notes
|
||||||
|
|
||||||
* `fastjson` is not a drop-in replacement for the built-in functions `JSON.parse()` and `JSON.stringify()` specified in [ECMA-262§§24.5.1-2](https://www.ecma-international.org/ecma-262/11.0/index.html#sec-json-object).
|
* `fastjson` is not a drop-in replacement for the built-in functions `JSON.parse()` and `JSON.stringify()` specified in [ECMA-262§§24.5.1-2](https://www.ecma-international.org/ecma-262/11.0/index.html#sec-json-object).
|
||||||
|
|||||||
42
benchmarks/index.js
Normal file
42
benchmarks/index.js
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
const fastjson = require('../src/index.js')
|
||||||
|
const chance = new (require('chance'))()
|
||||||
|
const Benchmark = require('benchmark')
|
||||||
|
|
||||||
|
const cyan = '\x1b[1m\x1b[36m'
|
||||||
|
const reset = '\x1b[0m'
|
||||||
|
|
||||||
|
console.log('Running benchmarks...')
|
||||||
|
|
||||||
|
/* Generate big array */
|
||||||
|
const arr = new Array(2 ** 8).fill().map(() => {
|
||||||
|
const obj = {}
|
||||||
|
|
||||||
|
for (let i = 0; i < 2 ** 8; i++) {
|
||||||
|
const key = chance.string({ length: 16 })
|
||||||
|
const value = chance.string({ length: 64 })
|
||||||
|
obj[key] = value
|
||||||
|
}
|
||||||
|
return obj
|
||||||
|
})
|
||||||
|
|
||||||
|
const str = JSON.stringify(arr)
|
||||||
|
|
||||||
|
function runSuite (func, arg, cb) {
|
||||||
|
const suite = new Benchmark.Suite()
|
||||||
|
suite.add(`fastjson.${func}`, () => {
|
||||||
|
fastjson[func](arg)
|
||||||
|
}).add(`JSON.${func}`, () => {
|
||||||
|
JSON[func](arg)
|
||||||
|
}).on('cycle', (event) => {
|
||||||
|
console.log(String(event.target))
|
||||||
|
}).on('complete', function () {
|
||||||
|
console.log(`${cyan}Fastest is: ${this.filter('fastest').map('name')}${reset}`)
|
||||||
|
if (cb) {
|
||||||
|
cb()
|
||||||
|
}
|
||||||
|
}).run({ async: true })
|
||||||
|
}
|
||||||
|
|
||||||
|
runSuite('stringify', arr, () => {
|
||||||
|
runSuite('parse', str, null)
|
||||||
|
})
|
||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "fastjson",
|
"name": "fastjson",
|
||||||
"version": "2.0.1",
|
"version": "2.0.2",
|
||||||
"description": "A high-performance, standards-compliant JSON serialiser/deserialiser for JavaScript.",
|
"description": "A high-performance, standards-compliant JSON serialiser/deserialiser for JavaScript.",
|
||||||
"homepage": "https://github.com/qntm/fastjson",
|
"homepage": "https://github.com/qntm/fastjson",
|
||||||
"repository": {
|
"repository": {
|
||||||
@ -19,9 +19,12 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"postpublish": "npm version patch && git push",
|
"postpublish": "npm version patch && git push",
|
||||||
"pretest": "npx standard",
|
"pretest": "npx standard",
|
||||||
"test": "npx jest"
|
"test": "npx jest",
|
||||||
|
"bench": "node ./benchmarks/index.js"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"benchmark": "^2.1.4",
|
||||||
|
"chance": "^1.1.7",
|
||||||
"jest": "^26.6.3",
|
"jest": "^26.6.3",
|
||||||
"standard": "^16.0.3"
|
"standard": "^16.0.3"
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user