mirror of https://github.com/qntm/fastjson
Compare commits
24 Commits
Author | SHA1 | Date |
---|---|---|
![]() |
7ee91b3ec5 | |
![]() |
84aa5f6b3a | |
![]() |
a99a06f95a | |
![]() |
81b0236817 | |
![]() |
0959f2acca | |
![]() |
2ff848538d | |
![]() |
13a466dd48 | |
![]() |
ff994ba9c2 | |
![]() |
42a6045c5a | |
![]() |
8929f395cb | |
![]() |
b1a5021bb6 | |
![]() |
926b49cd59 | |
![]() |
01b83a706d | |
![]() |
feb2ce2fce | |
![]() |
5a12fe83c0 | |
![]() |
0704728438 | |
![]() |
d8a67ef404 | |
![]() |
e757f4146d | |
![]() |
ca43f6114f | |
![]() |
b6450a80a9 | |
![]() |
c9ccb9d6b7 | |
![]() |
af388a2673 | |
![]() |
44ed7de089 | |
![]() |
d743846a53 |
|
@ -1,6 +1,11 @@
|
|||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: "npm"
|
||||
directory: "/"
|
||||
- package-ecosystem: 'npm'
|
||||
directory: '/'
|
||||
schedule:
|
||||
interval: "weekly"
|
||||
interval: 'monthly'
|
||||
ignore:
|
||||
- dependency-name: '*'
|
||||
update-types:
|
||||
- 'version-update:semver-minor'
|
||||
- 'version-update:semver-patch'
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
name: 'Travis CI replacement'
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches:
|
||||
- '**'
|
||||
|
||||
jobs:
|
||||
build-job:
|
||||
runs-on: 'ubuntu-latest'
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: ['14.x', '16.x', '18.x', '20.x', '22.x']
|
||||
|
||||
steps:
|
||||
- uses: 'actions/checkout@v2'
|
||||
|
||||
- name: 'Use Node.js ${{ matrix.node-version }}'
|
||||
uses: 'actions/setup-node@v1'
|
||||
with:
|
||||
node-version: '${{ matrix.node-version }}'
|
||||
|
||||
- name: 'Actual npm tasks'
|
||||
run: |
|
||||
npm install
|
||||
npm run test
|
|
@ -45,4 +45,3 @@ Temporary Items
|
|||
# mine
|
||||
coverage
|
||||
node_modules
|
||||
package-lock.json
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
language: node_js
|
||||
node_js:
|
||||
- 12
|
||||
- 14
|
|
@ -1,5 +1,9 @@
|
|||
# CHANGELOG
|
||||
|
||||
## 3.x.x
|
||||
|
||||
`fastjson` now distributes ES modules, not CommonJS modules.
|
||||
|
||||
## 2.x.x
|
||||
|
||||
Support for Node.js 0.10, 0.12, 4, 6, 8 and 10 is dropped.
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2021 qntm
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
21
README.md
21
README.md
|
@ -20,20 +20,20 @@ npm install fastjson
|
|||
## Usage
|
||||
|
||||
```js
|
||||
const fastjson = require('fastjson')
|
||||
import { parse, stringify } from 'fastjson'
|
||||
|
||||
const str = '{ "key": "value" }'
|
||||
const obj = fastjson.parse(str)
|
||||
const obj = parse(str)
|
||||
console.log(obj)
|
||||
|
||||
const obj2 = { key: 'value' }
|
||||
const str2 = fastjson.stringify(obj2)
|
||||
const str2 = stringify(obj2)
|
||||
console.log(str2)
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### fastjson.parse()
|
||||
### parse
|
||||
|
||||
[RFC 7159§9](https://tools.ietf.org/html/rfc7159#section-9) states:
|
||||
|
||||
|
@ -41,9 +41,9 @@ console.log(str2)
|
|||
>
|
||||
> A JSON parser transforms a JSON text into another representation. A JSON parser MUST accept all texts that conform to the JSON grammar. A JSON parser MAY accept non-JSON forms or extensions.
|
||||
|
||||
How this other representation should be constructed is not specified. The method `fastjson.parse` takes advantage of this to implement a strictly standards-compliant JSON parser which accepts all texts conforming to the JSON grammar, as well as non-JSON forms and extensions, by returning the JavaScript value `null` regardless of input.
|
||||
How this other representation should be constructed is not specified. `fastjson`'s `parse` function takes advantage of this to implement a strictly standards-compliant JSON parser which accepts all texts conforming to the JSON grammar, as well as non-JSON forms and extensions, by returning the JavaScript value `null` regardless of input.
|
||||
|
||||
### fastjson.stringify()
|
||||
### stringify
|
||||
|
||||
[RFC 7159§10](https://tools.ietf.org/html/rfc7159#section-10) states, in its entirety:
|
||||
|
||||
|
@ -51,11 +51,12 @@ How this other representation should be constructed is not specified. The method
|
|||
>
|
||||
> A JSON generator produces JSON text. The resulting text MUST strictly conform to the JSON grammar.
|
||||
|
||||
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`'s `stringify` function 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
|
||||
`fastjson`'s `parse` and `stringify` functions are between 4,000,000 and 40,000,000 times faster than the 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/).
|
||||
|
||||
* `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).
|
||||
## Note
|
||||
|
||||
`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).
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
const fastjson = require('../src/index.js')
|
||||
const chance = new (require('chance'))()
|
||||
const Benchmark = require('benchmark')
|
||||
import benchmark from 'benchmark'
|
||||
import Chance from 'chance'
|
||||
|
||||
const cyan = '\x1b[1m\x1b[36m'
|
||||
const reset = '\x1b[0m'
|
||||
import * as fastjson from '../src/index.js'
|
||||
|
||||
console.log('Running benchmarks...')
|
||||
const chance = new Chance()
|
||||
|
||||
/* Generate big array */
|
||||
// Generate big array
|
||||
const arr = new Array(2 ** 8).fill().map(() => {
|
||||
const obj = {}
|
||||
|
||||
|
@ -16,27 +14,36 @@ const arr = new Array(2 ** 8).fill().map(() => {
|
|||
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 })
|
||||
}
|
||||
console.log('Running benchmarks...')
|
||||
|
||||
runSuite('stringify', arr, () => {
|
||||
runSuite('parse', str, null)
|
||||
const runSuite = (func, arg) => new Promise(resolve => {
|
||||
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(`Fastest is: ${this.filter('fastest').map('name')}`)
|
||||
console.log()
|
||||
resolve()
|
||||
})
|
||||
.run({ async: true })
|
||||
})
|
||||
|
||||
Promise.resolve().then(async () => {
|
||||
await runSuite('stringify', arr)
|
||||
await runSuite('parse', str)
|
||||
})
|
||||
|
|
|
@ -1,17 +0,0 @@
|
|||
module.exports = {
|
||||
verbose: true,
|
||||
bail: false,
|
||||
collectCoverage: true,
|
||||
collectCoverageFrom: [
|
||||
'src/*.js',
|
||||
'!src/*.spec.js'
|
||||
],
|
||||
coverageThreshold: {
|
||||
global: {
|
||||
branches: 100,
|
||||
functions: 100,
|
||||
lines: 100,
|
||||
statements: 100
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
17
package.json
17
package.json
|
@ -1,13 +1,14 @@
|
|||
{
|
||||
"name": "fastjson",
|
||||
"version": "2.0.1",
|
||||
"version": "3.0.1",
|
||||
"description": "A high-performance, standards-compliant JSON serialiser/deserialiser for JavaScript.",
|
||||
"homepage": "https://github.com/qntm/fastjson",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/qntm/fastjson.git"
|
||||
},
|
||||
"main": "src",
|
||||
"type": "module",
|
||||
"main": "src/index.js",
|
||||
"keywords": [
|
||||
"json"
|
||||
],
|
||||
|
@ -17,15 +18,17 @@
|
|||
"src"
|
||||
],
|
||||
"scripts": {
|
||||
"bench": "node ./benchmarks/index.js",
|
||||
"mocha": "c8 --100 mocha",
|
||||
"postpublish": "npm version patch && git push",
|
||||
"pretest": "npx standard",
|
||||
"test": "npx jest",
|
||||
"bench": "node ./benchmarks/index.js"
|
||||
"standard": "standard",
|
||||
"test": "npm run standard && npm run mocha"
|
||||
},
|
||||
"devDependencies": {
|
||||
"benchmark": "^2.1.4",
|
||||
"c8": "^10.1.2",
|
||||
"chance": "^1.1.7",
|
||||
"jest": "^26.6.3",
|
||||
"standard": "^16.0.3"
|
||||
"mocha": "^11.0.0",
|
||||
"standard": "^17.0.0"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,2 @@
|
|||
module.exports = {
|
||||
parse: () => null,
|
||||
stringify: () => 'null'
|
||||
}
|
||||
export const parse = () => null
|
||||
export const stringify = () => 'null'
|
||||
|
|
|
@ -1,17 +0,0 @@
|
|||
/* eslint-env jest */
|
||||
|
||||
const fastjson = require('.')
|
||||
|
||||
describe('fastjson', () => {
|
||||
describe('parse', () => {
|
||||
it('works', () => {
|
||||
expect(fastjson.parse('abdsfsal{}')).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('stringify', () => {
|
||||
it('works', () => {
|
||||
expect(fastjson.stringify('lm995')).toBe('null')
|
||||
})
|
||||
})
|
||||
})
|
|
@ -0,0 +1,19 @@
|
|||
/* eslint-env mocha */
|
||||
|
||||
import assert from 'assert'
|
||||
|
||||
import { parse, stringify } from '../src/index.js'
|
||||
|
||||
describe('fastjson', () => {
|
||||
describe('parse', () => {
|
||||
it('works', () => {
|
||||
assert.deepStrictEqual(parse('abdsfsal{}'), null)
|
||||
})
|
||||
})
|
||||
|
||||
describe('stringify', () => {
|
||||
it('works', () => {
|
||||
assert.deepStrictEqual(stringify('lm995'), 'null')
|
||||
})
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue