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 🙏

© 2024 – Pkg Stats / Ryan Hefner

fast-clone-json

v1.1.0

Published

Clone JSON value fast

Downloads

117

Readme

Fastest JSON deep clone in JavaScript

deno land npm CI CC0 1.0

This project is based on fast-json-clone.

fast-clone-json is a tiny library to clone JSON values in pure JavaScript focusing on the speed. According to the benchmark, this package seems the fastest among several deep-clone implementations.

import * as assert from "assert";
import { cloneJson } from "fast-clone-json";

const value = {
  str: "hello",
  num: 42,
  array: [true, null],
  object: { str: "hello", bool: true },
};

const cloned = cloneJson(value);

assert.deepStrictEqual(value, cloned);
assert.notStrictEqual(value, cloned);
assert.notStrictEqual(value.array, cloned.array);
assert.notStrictEqual(value.object, cloned.object);

Installation

This package is licensed with CC0-1.0. Directly copy src/clone.ts to your project.

Or install via npm package manager.

npm install --save fast-clone-json

API

This package exports cloneJson() and cloneJsonObject() functions.

cloneJson(x: JsonValue): JsonValue;

This function clones the given JSON value and returns it. There are some invariants for the parameter:

  • The parameter must not contain circles as JSON does not allow it. This function will cause infinite loop for such values by design.
  • The parameter must contain JSON values only. Other values like Date, Regexp, Map, Set, Buffer, ... are not allowed.
cloneJsonObject(x: JsonObject): JsonObject;

This function clones the given JSON object and returns it. There are additional invariants for the parameter:

  • All invariants of cloneJson().
  • Directly specified parameters must be arrays or objects. Primitive values such as string cannot be specified.

Benchmark

Here is the benchmark result with large (1.2MB) JSON value. This package is the fastest among the implementations. Please see the benchmark directory for more details and other results.

Performance large

Native structuredClone             x 104 ops/sec ±0.40% (76 runs sampled)
structuredClone polyfill           x 105 ops/sec ±0.59% (77 runs sampled)
JSON serialize/deserialize         x 98.93 ops/sec ±0.69% (72 runs sampled)
Naive deep clone                   x 208 ops/sec ±0.18% (88 runs sampled)
lodash.clonedeep                   x 123 ops/sec ±2.42% (80 runs sampled)
clone-deep                         x 194 ops/sec ±0.38% (89 runs sampled)
rfdc (default)                     x 481 ops/sec ±0.19% (93 runs sampled)
rfdc (proto)                       x 551 ops/sec ±0.26% (92 runs sampled)
fastest-json-copy                  x 561 ops/sec ±0.15% (94 runs sampled)
fast-json-clone                    x 584 ops/sec ±0.15% (95 runs sampled)
fast-clone-json (cloneJson)        x 591 ops/sec ±0.24% (95 runs sampled)
fast-clone-json (cloneJsonObject)  x 592 ops/sec ±0.24% (93 runs sampled)

Fastest is fast-clone-json (cloneJsonObject),fast-clone-json (cloneJson)

FAQ

Why is this package the fastest?

Since this package is optimized for removing non-inline function calls in the hot loop as much as possible. rfdc is implemented with the same strategy.

Then why is this package faster than rfdc?

Since this package provides less functionalities. rfdc provides support for some non-JSON types (Date, Regexp, ...). It increases the number of branches in the hot loop so it causes trade-off in performance.

Why are my benchmark results different?

You are right. Improvements in this package are minor. In some cases it may be slower. (eg, differences in CPUs and Javascript engines.)

License

This package is distributed under CC0 1.0 (Public Domain). Feel free to copy and paste the implementation directly to your project without any copyright notice.