quick-format-unescaped
Advanced tools
Comparing version 2.0.1 to 3.0.0
@@ -5,4 +5,2 @@ var bench = require('fastbench') | ||
var lowres = {lowres: true} | ||
var run = bench([ | ||
@@ -13,8 +11,4 @@ function util(cb) { | ||
}, | ||
function quickLowres(cb) { | ||
quickFormat(['%s %j %d', 'a', {a: {x: 1}}, 1], lowres) | ||
setImmediate(cb) | ||
}, | ||
function quick(cb) { | ||
quickFormat(['%s %j %d', 'a', {a: {x: 1}}, 1], null) | ||
quickFormat('%s %j %d', 'a', [{a: {x: 1}}, 1], null) | ||
setImmediate(cb) | ||
@@ -26,8 +20,4 @@ }, | ||
}, | ||
function quickWithTailObjLowres(cb) { | ||
quickFormat(['hello %s %j %d', 'world', {obj: true}, 4, {another: 'obj'}], lowres) | ||
setImmediate(cb) | ||
}, | ||
function quickWithTailObj(cb) { | ||
quickFormat(['hello %s %j %d', 'world', {obj: true}, 4, {another: 'obj'}], null) | ||
quickFormat('hello %s %j %d', 'world', [{obj: true}, 4, {another: 'obj'}], null) | ||
setImmediate(cb) | ||
@@ -34,0 +24,0 @@ } |
25
index.js
@@ -6,8 +6,17 @@ 'use strict' | ||
module.exports = function format(args, opts) { | ||
module.exports = format | ||
function format(f, args, opts) { | ||
var ss = (opts && opts.stringify) || tryStringify | ||
var f = args[0] | ||
if (typeof f !== 'string') { | ||
var objects = new Array(args.length) | ||
for (var index = 0; index < args.length; index++) { | ||
var offset = 1 | ||
if (f === null) { | ||
f = args[0] | ||
offset = 0 | ||
} | ||
if (typeof f === 'object' && f !== null) { | ||
var len = args.length + offset | ||
if (len === 1) return f | ||
var objects = new Array(len) | ||
objects[0] = ss(f) | ||
for (var index = 1; index < len; index++) { | ||
objects[index] = ss(args[index]) | ||
@@ -17,9 +26,7 @@ } | ||
} | ||
var argLen = args.length | ||
if (argLen === 1) return f | ||
if (argLen === 0) return f | ||
var x = '' | ||
var str = '' | ||
var a = 1 | ||
var a = 1 - offset | ||
var lastPos = 0 | ||
@@ -26,0 +33,0 @@ var flen = f.length |
{ | ||
"name": "quick-format-unescaped", | ||
"version": "2.0.1", | ||
"version": "3.0.0", | ||
"description": "Solves a problem with util.format", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,5 +0,3 @@ | ||
# quick format unescaped | ||
# quick-format-unescaped | ||
Solves a problem with util.format | ||
## unescaped ? | ||
@@ -14,4 +12,3 @@ | ||
var format = require('quick-format') | ||
var options = {lowres: false} // <--default | ||
format(['hello %s %j %d', 'world', {obj: true}, 4, {another: 'obj'}], options) | ||
format('hello %s %j %d', 'world', [{obj: true}, 4, {another: 'obj'}]) | ||
``` | ||
@@ -21,50 +18,39 @@ | ||
### lowres | ||
### stringify | ||
Passing an options object with `lowres: true` will cause quick-format any object with a circular as a string with the value '"[Circular]"'. The default behaviour is to label | ||
circular references in an object, instead of abandoning the entire object. Naturally, | ||
`lowres` is a faster mode, and assumes you have made the decision to ensure the objects | ||
you're passing have no circular references. | ||
Passing an options object as the third parameter with a `stringify` will mean | ||
any objects will be passed to the supplied function instead of an the | ||
internal `tryStringify` function. This can be useful when using augmented | ||
capability serializers such as [`fast-safe-stringify`](http://github.com/davidmarkclements/fast-safe-stringify) or [`fast-redact`](http://github.com/davidmarkclements/fast-redact). | ||
## caveats | ||
We use `JSON.stringify` instead of `util.inspect`, this means object | ||
methods (functions) *will not be serialized*. | ||
By default `quick-format-unescaped` uses `JSON.stringify` instead of `util.inspect`, this means functions *will not be serialized*. | ||
## util.format | ||
## Benchmarks | ||
In `util.format` for Node 5.9, performance is significantly affected | ||
when we pass in more arguments than interpolation characters, e.g | ||
### Node 8.11.2 | ||
```js | ||
util.format('hello %s %j %d', 'world', {obj: true}, 4, {another: 'obj'}) | ||
``` | ||
util*100000: 350.325ms | ||
quick*100000: 268.141ms | ||
utilWithTailObj*100000: 586.387ms | ||
quickWithTailObj*100000: 280.200ms | ||
util*100000: 325.735ms | ||
quick*100000: 270.251ms | ||
utilWithTailObj*100000: 492.270ms | ||
quickWithTailObj*100000: 261.797ms | ||
``` | ||
This is mostly due to the use of `util.inspect`. Use `JSON.stringify` | ||
(safely) instead which is significantly faster. | ||
### Node 10.4.0 | ||
It also takes an array instead of arguments, which helps us | ||
avoid the use of `apply` in some cases. | ||
Also - for speed purposes, we ignore symbol. | ||
## Benchmarks | ||
Whilst exact matching of objects to interpolation characters is slower, | ||
the case of additional objects is 3x faster. Further, using `lowres` mode | ||
brings us closer to `util.inspect` speeds. | ||
``` | ||
util*100000: 205.978ms | ||
quickLowres*100000: 236.337ms | ||
quick*100000: 292.018ms | ||
utilWithTailObj*100000: 1054.592ms | ||
quickWithTailObjLowres*100000: 267.992ms | ||
quickWithTailObj*100000: 343.048ms | ||
util*100000: 212.011ms | ||
quickLowres*100000: 226.441ms | ||
quick*100000: 296.600ms | ||
utilWithTailObj*100000: 1020.195ms | ||
quickWithTailObjLowres*100000: 267.331ms | ||
quickWithTailObj*100000: 343.867ms | ||
util*100000: 301.035ms | ||
quick*100000: 217.005ms | ||
utilWithTailObj*100000: 404.778ms | ||
quickWithTailObj*100000: 236.176ms | ||
util*100000: 286.349ms | ||
quick*100000: 214.646ms | ||
utilWithTailObj*100000: 388.574ms | ||
quickWithTailObj*100000: 226.036ms | ||
``` | ||
@@ -74,2 +60,2 @@ | ||
Sponsored by nearForm | ||
Sponsored by [nearForm](http://www.nearform.com) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8636
172
59