yarabey / stringifyit

Fast object stringifier

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

stringifyit

Fast node.js stringify library with sorting and typing. Provides Stringifier class. Stringifier provides stringify Symbol to allow you customize stringifying your own classes.

See benchmarks for compare to other libs.

Install

npm i stringifyit --save

Features

  • Supports node.js >= 4.0.0
  • Supports Map/WeakMap, Set/WeakSet and typed arrays
  • Supports sort Set, Map, object keys and optional sort arrays
  • Supports custom stringify rules for user-defined classes
  • Useful for browsers
  • Very fast stringify library

API

Classes

Stringifier

Provides interface to stringify any value Sort Map, Set and object keys by default without ability to avoid it

Members

stringify : stringify

Functions

stringifyit(value, [options])string

Helper for simple stringify single value

Stringifier

Provides interface to stringify any value Sort Map, Set and object keys by default without ability to avoid it

Kind: global class

new Stringifier([options])

Param Type
[options] options

stringifier.string : string

Accumulator string

Kind: instance property of Stringifier
Access: public

stringifier.update(value)

Stringifies value and append it to current accumulator string

Kind: instance method of Stringifier

Param Type
value *

Stringifier~stringifyCallback : function

Custom stringify callback declared with stringify Symbol

Kind: inner typedef of Stringifier

Param Type Description
stringifier Stringifier Stringifier instance

Example

const {stringify} = require('stringifyit');
CustomType.prototype[stringify] = function (stringifier) {
    stringifier.string += 'start';

    stringifier.update(this.someProp);
    stringifier.update(['use', 'any', 'type']);

    stringifier.string += 'end';
}

Stringifier~stringify : Symbol

Symbol to add custom stringify rules for user types

Kind: inner typedef of Stringifier

Stringifier~options : Object

Stringifier options

Kind: inner typedef of Stringifier
Properties

Name Type Description
sortArrays boolean Sort arrays before stringify
includePrimitiveTypes boolean Stringify primitive values (and functions) types
includeConstructorNames boolean Stringify non-primitive values constructor names

stringify : stringify

Kind: global variable

stringifyit(value, [options]) ⇒ string

Helper for simple stringify single value

Kind: global function

Param Type
value *
[options] options

Example

const {stringifyit} = require('stringifyit');

stringifyit({key: 'value', value: 'key'}) === stringifyit({value: 'key', key: 'value'}); // true
stringifyit(new Set(['value1', 'value2'])) === stringifyit(new Set(['value2', 'value1'])); // true
stringifyit(new Map([['key', 'value'], ['value', 'key']])) === stringifyit(new Map([['value', 'key'], ['key', 'value']])); // true
stringifyit([1, 2, 3]) === stringifyit([1, 2, 3]); // true
stringifyit([1, 2, 3], {sortArrays: true}) === stringifyit([1, 3, 2], {sortArrays: true}); // true

stringifyit([1, 2, 3]) === stringifyit([1, 3, 2]); // false
stringifyit(5) === stringifyit('5'); // false

Custom stringifiers source

Object.prototype[stringify] : stringifyCallback

Array.prototype[stringify] : stringifyCallback

TypedArray.prototype[stringify] : stringifyCallback

Map.prototype[stringify] : stringifyCallback

WeakMap.prototype[stringify] : stringifyCallback

Set.prototype[stringify] : stringifyCallback

WeakSet.prototype[stringify] : stringifyCallback

Date.prototype[stringify] : stringifyCallback

Benchmarks

Benchmarked with Node.js v6.9.5

Usage

  • npm run bench to run benchmarking stringifyit operations/second for different cases

Results

array x 1,947,707 ops/sec ±1.60% (85 runs sampled)
object x 2,366,530 ops/sec ±1.30% (89 runs sampled)
nestedObject x 29,384 ops/sec ±1.48% (87 runs sampled)
complexObject_5items x 35,847 ops/sec ±1.87% (87 runs sampled)
complexObject_10items x 18,407 ops/sec ±2.03% (87 runs sampled)
complexObject_100items x 1,682 ops/sec ±2.09% (85 runs sampled)
set x 215,921 ops/sec ±2.52% (84 runs sampled)
map x 190,451 ops/sec ±2.57% (84 runs sampled)

License

MIT

About

Fast object stringifier


Languages

Language:JavaScript 100.0%