@ 喜悦的垂直/ json
高性能的JSON与Rust SIMD加速度和自动倒置一起解析并串行.
特征
- 空投替换 页:1
JSON.parse()财务报告和审定财务报表JSON.stringify() - 2 -3x 速度快一点 在支持的平台上分析 SIMD 加速
- 自动倒置 无法使用 Rust 绑定时切入本地 JavaScript
- 深克隆 函数已优化
JSON.parse(JSON.stringify())模式 - 安全变种 不要犯错
- 先输入脚本 带有完整类型定义
安装
npm install @happyvertical/json
# or
pnpm add @happyvertical/json
快速启动
import { parse, stringify, clone } from '@happyvertical/json';
// Drop-in replacements for JSON.parse/stringify
const data = parse<User>('{"name": "Alice", "age": 30}');
const json = stringify(data);
// Deep clone (optimized)
const copy = clone(data);
copy.age = 31; // Original unchanged
API 密码
空投替换
// Parse JSON string to value
function parse<T = unknown>(text: string, reviver?: Reviver): T;
// Stringify value to JSON
function stringify(value: unknown, replacer?: Replacer, space?: number | string): string;
// Deep clone via JSON round-trip (optimized)
function clone<T>(value: T): T;
安全替代品(不投掷)
import { safeParse, safeStringify } from '@happyvertical/json';
// Returns { success: true, value: T } or { success: false, error: ParseError }
const result = safeParse<User>(maybeInvalidJson);
if (result.success) {
console.log(result.value.name);
} else {
console.error(`Line ${result.error.line}: ${result.error.message}`);
}
// Returns { success: true, value: string } or { success: false, error: StringifyError }
const stringifyResult = safeStringify(data);
验证
import { isValid } from '@happyvertical/json';
if (isValid(userInput)) {
const data = parse(userInput);
}
适配器控制
import { JSONFactory, getAdapterInfo, isSIMDAvailable } from '@happyvertical/json';
// Check if SIMD acceleration is available
console.log(isSIMDAvailable()); // true on supported platforms
// Get detailed adapter info
const info = getAdapterInfo();
console.log(info.name); // 'sonic' or 'native'
console.log(info.simdEnabled); // true if using SIMD
// Create adapter with specific options
const json = JSONFactory.create({
adapter: 'sonic', // 'sonic' | 'native' | 'auto'
fallback: true, // Fall back to native if sonic unavailable
});
// Use adapter directly
const data = json.parse('{"key": "value"}');
const text = json.stringify(data);
业绩
在680KB 显示文件(典型的 SMRT 显示文件):
| 运行 | 本地 JSON | @ 喜悦的垂直/ json | 加速 |
|---|---|---|---|
| 解析 | ~50 ms 时 | ~18ms 时 | 2.8x 单曲 |
| 字符串 | ~45ms (英语) | ~16,000米) | 2.8x 单曲 |
| 克隆 | ~95ms (单位:千米) | ~34ms 时 | 2.8x 单曲 |
在Apple M2 Pro上测量的结果,性能因平台和数据结构而异.
平台支持
SIMD-加速解析可在:
- & macOS ARM64 (苹果硅)
- * macOS x64(英特尔)
- XQ Linux x64 (GNU) (英语)
- Linux ARM64 (GNU) (英语)
- QQ Linux x64( 穆尔)
- Linux ARM64( 穆斯尔)
- 视窗x64
当本地二进制不可用时, 软件包会自动倒回 JavaScript 的内置 JSON 对象为零.
如何运作
此软件包使用 音响 - Rust JSON图书馆,使用SIMD指令(AVX2、SSE4.2、NEON)进行矢量分解和序列化.
适配器模式允许:
- 自动选择可用最快的执行程序
- 原始束缚无法装入时, 优雅地回落
- 一致的API,无论基本执行
类型脚本支持
完整类型脚本定义包括:
interface User {
name: string;
age: number;
}
// Type-safe parsing
const user = parse<User>('{"name": "Alice", "age": 30}');
user.name; // string
user.age; // number
// Safe variant preserves types
const result = safeParse<User>(json);
if (result.success) {
result.value.name; // string
}
退缩和替换
标准复活器和替换器功能按预期工作:
// Reviver for custom transformations
const data = parse<{ date: Date }>(
'{"date": "2024-01-15"}',
(key, value) => (key === 'date' ? new Date(value) : value)
);
console.log(data.date instanceof Date); // true
// Replacer for filtering
const json = stringify(data, ['name', 'email']); // Only include these keys
// Pretty printing
const pretty = stringify(data, null, 2);
环境变量
HAPPYVERTICAL_JSON_ADAPTER: 强制适配器选择 ("音速" QQ"内置")
许可证
麻省理工学院