npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

nv-random-sml-json

v1.0.2

Published

## 概述

Readme

nv-random-sml-json - 随机 小JSON 生成器

概述

nv-random-sml-json 随机 小JSON 生成器,用于生成各种类型的 JSON 数据用于测试和模拟。

功能特性

  • 三种使用方式:Generator、Callback、批量生成
  • 性能:37万+ JSON/秒的吞吐量
  • 混合类型:支持 string、number、boolean、null、object、array
  • 可配置:BATCH 大小可调节
  • TypeScript 支持:完整的类型定义

性能测试结果

测试环境

  • 测试数量:100,000 个 JSON
  • 测试轮数:5 轮平均
  • BATCH 大小:8192(最优值)

性能对比

| 方法 | 平均耗时(ms)| 吞吐量(JSON/秒)| 相对最快 | |---------------|-------------|----------------|-----------| | Generator | 268.43 | 372,544 | 100.0% ⭐ | | Once | 270.22 | 370,066 | 100.7% | | Callback | 285.70 | 350,018 | 106.4% |

性能分析

  • Generator 方式最快:适合逐个生成场景
  • Once 方式接近:仅慢 0.7%,适合批量生成场景
  • Callback 方式略慢:函数调用开销导致慢 6.4%

BATCH 大小优化

经过对 17 种不同 BATCH 大小的测试(1 到 32768),BATCH=8192 达到最优性能:

  • BATCH 太小(1-4):函数调用开销大
  • BATCH=8192:最优平衡点
  • BATCH 更大:性能略有下降

使用方法

1. Generator 方式(默认,推荐)

const genJson = require('nv-random-sml-json');

// 逐个生成 JSON
const json1 = genJson();
const json2 = genJson();
const json3 = genJson();

console.log(json1); // 可能是字符串、数字、对象、数组等

适用场景:

  • 需要逐个生成
  • 惰性求值
  • 流式处理

2. 批量生成方式(推荐用于批量场景)

const genJson = require('nv-random-sml-json');

// 一次生成 1000 个 JSON
const jsons = genJson.once(1000);

console.log(`生成了 ${jsons.length} 个 JSON`);
console.log('第一个:', jsons[0]);

适用场景:

  • 批量生成
  • 测试数据准备
  • 性能测试

性能特点:

  • 与 Generator 方式性能几乎相同
  • 内存一次性分配,避免动态扩展
  • 返回数组,方便批量处理

3. Callback 方式

const genJson = require('nv-random-sml-json');

let count = 0;
genJson.using_callback((json) => {
    console.log(`#${++count}:`, json);
    
    // 返回 true 停止生成
    if (count >= 100) {
        return true;
    }
});

适用场景:

  • 需要逐个处理
  • 条件终止
  • 自定义处理逻辑

配置

调整 BATCH 大小

const genJson = require('nv-random-sml-json');

// 查看当前 BATCH
console.log('当前 BATCH:', genJson.BATCH); // 默认 8192

// 修改 BATCH(不推荐,除非有特殊需求)
genJson.BATCH = 4096;

注意: BATCH=8192 是经过性能测试的最优值,不建议修改。

生成的数据类型

生成器会随机产生以下类型的值:

  • 基本类型

    • string - 随机字符串(长度 0-30)
    • number - SMI 整数或浮点数
    • boolean - true/false
    • null
  • 复合类型

    • object - 嵌套对象 {key: value, ...}
    • array - 嵌套数组 [value1, value2, ...]

示例输出

// 可能的输出示例:

"hello"                              // 字符串
42                                   // 数字
true                                 // 布尔值
null                                 // null
{ "name": "test", "age": 25 }       // 对象
[1, "two", null, { "nested": [] }]  // 数组

TypeScript 支持

import genJson, { once, using_callback, JsonValue } from 'nv-random-sml-json';

// 类型推断
const json: JsonValue = genJson();
const batch: JsonValue[] = once(100);

// Callback 类型安全
using_callback((json: JsonValue): boolean => {
    console.log(json);
    return false;
});

技术细节

V8 优化

经过字节码分析,生成器已获得 V8 的充分优化:

  • ✅ 热点函数已内联(rand_good_char, rand_bad_char
  • ✅ TurboFan 优化编译
  • ✅ 15 个 deopt 保护点(正常范围)
  • ✅ 类型推测优化

性能瓶颈

由于需要生成混合类型数组(PACKED_ELEMENTS),存在以下不可避免的开销:

  1. HeapNumber 分配 - 超出 SMI 范围的数字需要堆分配
  2. 字符串分配 - 每个字符串都需要内存分配
  3. 对象/数组构造 - 动态结构需要内存和属性管理
  4. GC 压力 - 频繁分配触发年轻代 GC

这些开销是合理且必要的,当前性能已接近理论上限。

性能建议

  1. 保持默认 BATCH=8192 - 已经过优化测试
  2. 避免频繁切换生成方式 - 保持热路径优化

测试脚本

项目包含以下测试脚本:

  • TEST/check.js - 验证生成分布的正确性
  • TEST/perf.js - token 生成器性能测试
  • TEST/perf-gen.js - JSON 生成器性能测试
  • TEST/cmp.js - 三种方式性能对比
  • TEST/test-final.js - 功能测试和结构验证
  • TEST/show-byte-code.js - V8 字节码和优化分析

性能测试

# 运行性能对比测试
node TEST/cmp.js 100000 5

# 测试不同 BATCH 大小
node TEST/perf-gen.js 100000

# V8 优化分析
node --trace-opt TEST/show-byte-code.js

License

MIT