Skip to content

Commit 578b5ab

Browse files
Refactor fastJsonFormat for high-performance JSON formatting and Unicode decoding (#3)
1 parent a28022f commit 578b5ab

File tree

5 files changed

+266
-218
lines changed

5 files changed

+266
-218
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"scripts": {
1919
"prepack": "npm run test",
2020
"benchmark": "node benchmark.js",
21-
"test": "jest tests/*.spec.js"
21+
"test": "jest"
2222
},
2323
"devDependencies": {
2424
"@faker-js/faker": "^9.9.0",

src/constants/index.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Lookup table for structural characters in JSON such as {}[],:"
3+
* @type {Uint8Array}
4+
*/
5+
const STRUCTURAL_CHARS = new Uint8Array(128);
6+
7+
/**
8+
* Lookup table for whitespace characters (tab, newline, carriage return, space)
9+
* @type {Uint8Array}
10+
*/
11+
const WHITESPACE_CHARS = new Uint8Array(128);
12+
13+
/**
14+
* Common JSON structural character codes.
15+
* @readonly
16+
* @enum {number}
17+
*/
18+
const CHAR_CODE = {
19+
QUOTE: 34, // "
20+
BACKSLASH: 92, // \
21+
SLASH: 47, // /
22+
OPEN_BRACE: 123, // {
23+
CLOSE_BRACE: 125, // }
24+
OPEN_BRACKET: 91, // [
25+
CLOSE_BRACKET: 93, // ]
26+
COMMA: 44, // ,
27+
COLON: 58, // :
28+
};
29+
30+
// Initialize lookup tables
31+
(() => {
32+
/** @type {number[]} JSON structural characters: " , : [ ] { } */
33+
const structuralCodes = [34, 44, 58, 91, 93, 123, 125];
34+
structuralCodes.forEach((code) => (STRUCTURAL_CHARS[code] = 1));
35+
36+
/** @type {number[]} Whitespace characters: \t \n \r space */
37+
const whitespaceCodes = [9, 10, 13, 32];
38+
whitespaceCodes.forEach((code) => (WHITESPACE_CHARS[code] = 1));
39+
})();
40+
41+
module.exports = { STRUCTURAL_CHARS, WHITESPACE_CHARS, CHAR_CODE };

0 commit comments

Comments
 (0)