@casual-simulation/fast-json-stable-stringify

中文文档
4.0.5发布于 2026/01/31MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 3101

deterministic JSON.stringify() - a faster version of substack's json-stable-strigify without jsonify

原始使用文档
文档目录6 节
  1. var str = stringify(obj, opts)
  2. options
  3. cmp
  4. cycles
  5. Enterprise support
  6. Security contact

fast-json-stable-stringify

Forked from https://github.com/epoberezkin/fast-json-stable-stringify. Changes include converting to TypeScript and adding the ability to indent JSON output.

Deterministic JSON.stringify() - a faster version of @substack's json-stable-strigify without jsonify.

You can also pass in a custom comparison function.

example

var stringify = require('fast-json-stable-stringify');
var obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 };
console.log(stringify(obj));

output:

{"a":3,"b":[{"x":4,"y":5,"z":6},7],"c":8}

methods

var stringify = require('fast-json-stable-stringify');

var str = stringify(obj, opts)

Return a deterministic stringified string str from the object obj.

options

cmp

If opts is given, you can supply an opts.cmp to have a custom comparison function for object keys. Your function opts.cmp is called with these parameters:

opts.cmp({ key: akey, value: avalue }, { key: bkey, value: bvalue });

For example, to sort on the object key names in reverse order you could write:

var stringify = require('fast-json-stable-stringify');

var obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 };
var s = stringify(obj, function (a, b) {
    return a.key < b.key ? 1 : -1;
});
console.log(s);

which results in the output string:

{"c":8,"b":[{"z":6,"y":5,"x":4},7],"a":3}

Or if you wanted to sort on the object values in reverse order, you could write:

var stringify = require('fast-json-stable-stringify');

var obj = { d: 6, c: 5, b: [{z:3,y:2,x:1},9], a: 10 };
var s = stringify(obj, function (a, b) {
    return a.value < b.value ? 1 : -1;
});
console.log(s);

which outputs:

{"d":6,"c":5,"b":[{"z":3,"y":2,"x":1},9],"a":10}

cycles

Pass true in opts.cycles to stringify circular property as __cycle__ - the result will not be a valid JSON string in this case.

TypeError will be thrown in case of circular object without this option.

install

With npm do:

npm install fast-json-stable-stringify

benchmark

To run benchmark (requires Node.js 6+):

node benchmark

Results:

fast-json-stable-stringify x 17,189 ops/sec ±1.43% (83 runs sampled)
json-stable-stringify x 13,634 ops/sec ±1.39% (85 runs sampled)
fast-stable-stringify x 20,212 ops/sec ±1.20% (84 runs sampled)
faster-stable-stringify x 15,549 ops/sec ±1.12% (84 runs sampled)
The fastest is fast-stable-stringify

Enterprise support

fast-json-stable-stringify package is a part of Tidelift enterprise subscription - it provides a centralised commercial support to open-source software users, in addition to the support provided by software maintainers.

Security contact

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure. Please do NOT report security vulnerability via GitHub issues.

license

MIT

版本与发布记录

@casual-simulation/fast-json-stable-stringify 共发布 29 个版本,首次发布于 2021/10/12;过去 12 个月发布了 7 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.0.5latest2026/01/319 个月前
4.0.1-alpha.21448844597alpha2026/01/299 个月前
4.0.02026/01/289 个月前
3.10.4-alpha.209347840182026/01/139 个月前
3.10.32026/01/0810 个月前
3.10.3-alpha.207875543102026/01/0810 个月前

其他发布通道:canary → 3.5.0-alpha.15494496163,alpha → 4.0.1-alpha.21448844597。

关于 @casual-simulation/fast-json-stable-stringify 的常见问题

@casual-simulation/fast-json-stable-stringify 是什么?

deterministic JSON.stringify() - a faster version of substack's json-stable-strigify without jsonify

如何安装 @casual-simulation/fast-json-stable-stringify?

在项目目录运行 npm install @casual-simulation/fast-json-stable-stringify,或使用 pnpm add / yarn add / bun add。

@casual-simulation/fast-json-stable-stringify 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@casual-simulation/fast-json-stable-stringify 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@casual-simulation/fast-json-stable-stringify 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=8。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@casual-simulation/fast-json-stable-stringify 可以商用吗?

@casual-simulation/fast-json-stable-stringify 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@casual-simulation/fast-json-stable-stringify 的下载量有多少?

根据 npm 官方下载统计,@casual-simulation/fast-json-stable-stringify 在 2026/10/09 – 2026/11/07 这 30 天内被下载 3,101 次,日均约 103 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@casual-simulation/fast-json-stable-stringify 还在维护吗?

最新版本 4.0.5 发布于 2026/01/31(9 个月前);至今共发布 29 个版本,其中过去 12 个月发布了 7 个;首次发布于 2021/10/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。