fast-format
Advanced tools
Comparing version 0.1.1 to 0.2.0
/** | ||
* @preserve fast-format.js (C) 2015 KNOWLEDGECODE | MIT | ||
* @preserve fast-format.js (C) KNOWLEDGECODE | MIT | ||
*/ | ||
@@ -8,9 +8,33 @@ (function (global) { | ||
var f = function (format) { | ||
var i, len, argc = arguments.length, v = (format + '').split('%s'), r = argc ? v[0] : ''; | ||
for (i = 1, len = v.length, argc--; i < len; i++) { | ||
r += (i > argc ? '%s' : arguments[i]) + v[i]; | ||
var argc = arguments.length, v = (argc ? format + '' : '').split('%s'), i = 1, len = v.length, r = v[0]; | ||
if (argc > len) { | ||
argc = len; | ||
} | ||
while (i < argc) { | ||
r += arguments[i] + v[i++]; | ||
} | ||
while (i < len) { | ||
r += '%s' + v[i++]; | ||
} | ||
return r; | ||
}; | ||
f.compile = function (format) { | ||
var v = (arguments.length ? format + '' : '').split('%s'), len = v.length; | ||
return function () { | ||
var i = 0, argc = arguments.length, r = v[0]; | ||
if (argc > len - 1) { | ||
argc = len - 1; | ||
} | ||
while (i < argc) { | ||
r += arguments[i] + v[++i]; | ||
} | ||
i++; | ||
while (i < len) { | ||
r += '%s' + v[i++]; | ||
} | ||
return r; | ||
}; | ||
}; | ||
if (typeof module === 'object' && typeof module.exports === 'object') { | ||
@@ -17,0 +41,0 @@ module.exports = f; |
/* | ||
fast-format.js (C) 2015 KNOWLEDGECODE | MIT | ||
fast-format.js (C) KNOWLEDGECODE | MIT | ||
*/ | ||
(function(a){var c=function(a){var b,c,d=arguments.length,e=(a+"").split("%s"),f=d?e[0]:"";b=1;c=e.length;for(d--;b<c;b++)f+=(b>d?"%s":arguments[b])+e[b];return f};"object"===typeof module&&"object"===typeof module.exports?module.exports=c:"function"===typeof a.define&&a.define.amd?a.define([],function(){return c}):a.format=c})(this); | ||
(function(a){var f=function(a){var c=arguments.length,d=(c?a+"":"").split("%s"),b=1,g=d.length,e=d[0];for(c>g&&(c=g);b<c;)e+=arguments[b]+d[b++];for(;b<g;)e+="%s"+d[b++];return e};f.compile=function(a){var c=(arguments.length?a+"":"").split("%s"),d=c.length;return function(){var b=0,a=arguments.length,e=c[0];for(a>d-1&&(a=d-1);b<a;)e+=arguments[b]+c[++b];for(b++;b<d;)e+="%s"+c[b++];return e}};"object"===typeof module&&"object"===typeof module.exports?module.exports=f:"function"===typeof a.define&& | ||
a.define.amd?a.define([],function(){return f}):a.format=f})(this); |
{ | ||
"name": "fast-format", | ||
"version": "0.1.1", | ||
"description": "A simplified version of Node.js util.format()", | ||
"version": "0.2.0", | ||
"description": "A fast, simple string formatter like util.format() method in Node.js", | ||
"main": "fast-format.js", | ||
@@ -11,8 +11,8 @@ "directories": { | ||
"expect.js": "^0.3.1", | ||
"mocha": "^2.2.5", | ||
"mocha-phantomjs": "^3.5.3" | ||
"mocha": "^2.5.3", | ||
"mocha-phantomjs": "^4.1.0" | ||
}, | ||
"scripts": { | ||
"test": "mocha test/test.js && mocha-phantomjs test/test.html", | ||
"compile": "./compile" | ||
"compile": "./compile.sh fast-format.js fast-format.min.js" | ||
}, | ||
@@ -19,0 +19,0 @@ "keywords": [ |
@@ -1,6 +0,8 @@ | ||
# fast-format [![Circle CI](https://circleci.com/gh/knowledgecode/fast-format.svg?style=shield)](https://circleci.com/gh/knowledgecode/fast-format) | ||
This is a simplified version of Node.js `util.format()`. This supports only `%s` placeholder, but faster than that. This will be the best solution if you need speed rather than complex formatting. | ||
# fast-format | ||
[![Circle CI](https://circleci.com/gh/knowledgecode/fast-format.svg?style=shield)](https://circleci.com/gh/knowledgecode/fast-format) | ||
This is a string formatter like `util.format()` method in Node.js, supports just only `%s` placeholder but accordingly faster than that. It will be one of the best solution if need a speed rather than complex formatting. | ||
## Usage | ||
Same as Node.js `util.format()`. | ||
Same as `util.format()` method. | ||
```js | ||
@@ -10,12 +12,25 @@ format(formatString[, ...]) | ||
If use one formatting repeatedly, recommended to compile the `formatString` in advance. | ||
```js | ||
format.compile(formatString) | ||
``` | ||
## Example | ||
```js | ||
var s = format('%s, %s!', 'Hello', 'world'); | ||
let s = format('%s, %s!', 'Hello', 'world'); | ||
console.log(s); // => 'Hello, world!' | ||
``` | ||
```js | ||
let f = format.compile('%s, %s!'); | ||
let s1 = f('Hello', 'world'); | ||
console.log(s1); // => 'Hello, world!' | ||
let s2 = f('Howdy', 'World'); | ||
console.log(s2); // => 'Howdy, World!' | ||
``` | ||
## Benchmark | ||
```js | ||
var i, len, s = Date.now(); | ||
for (i = 0, len = 10000000; i < len; i++) { | ||
// Bench 1 | ||
let s = Date.now(); | ||
for (let i = 0, len = 100000000; i < len; i++) { | ||
format('i = %s, len = %s', i, len); | ||
@@ -25,25 +40,34 @@ } | ||
``` | ||
```js | ||
// Bench 2 | ||
let s = Date.now(); | ||
let f = format.compile('i = %s, len = %s'); | ||
for (let i = 0, len = 100000000; i < len; i++) { | ||
f(i, len); | ||
} | ||
console.log(Date.now() - s); | ||
``` | ||
*environment1: MacBook Air Early 2015 + Node.js v0.12.5* | ||
*environment1: Core i7 2.2GHz + Node.js v6.9.5* | ||
<img src="https://rawgit.com/knowledgecode/fast-format/master/img/graph1.svg"> | ||
<img src="https://cdn.rawgit.com/knowledgecode/fast-format/ee4147a012f4d179c84e94a5f549f741fb5a5069/img/graph1.svg"> | ||
| module | time | | ||
|-------------|-------------| | ||
| fast-format | 2,072 msec | | ||
| util.format | 11,571 msec | | ||
| sprintf-js | 19,438 msec | | ||
| module | time | bench | | ||
|-------------|-------------|:-----:| | ||
| fast-format | 12,388 msec | 2 | | ||
| fast-format | 22,039 msec | 1 | | ||
| util.format | 28,659 msec | 1 | | ||
--- | ||
*environment2: Core i7 2.5GHz Windows 8.1 Pro + Internet Explorer 11* | ||
*environment2: Core i7 2.2GHz + Google Chrome 56.0.2924.87* | ||
<img src="https://rawgit.com/knowledgecode/fast-format/master/img/graph2.svg"> | ||
<img src="https://cdn.rawgit.com/knowledgecode/fast-format/ee4147a012f4d179c84e94a5f549f741fb5a5069/img/graph2.svg"> | ||
| module | time | | ||
|-------------|-------------| | ||
| fast-format | 25,302 msec | | ||
| util.format | 40,550 msec | | ||
| sprintf-js | 58,133 msec | | ||
| module | time | bench | | ||
|-------------|-------------|:-----:| | ||
| fast-format | 12,898 msec | 2 | | ||
| fast-format | 22,705 msec | 1 | | ||
| util.format | 99,103 msec | 1 | | ||
[sprintf-js](https://github.com/alexei/sprintf.js) is a JavaScript sprintf implementation for the browser and Node.js. It is slow but might not be inevitable because a high functional module. | ||
The `util.format()` method was converted with `Browserify` to run on the browser. | ||
@@ -56,3 +80,3 @@ ## Installation | ||
### via bower | ||
### via Bower | ||
```sh | ||
@@ -62,7 +86,11 @@ bower install fast-format | ||
### directly (in case of the browser) | ||
``` html | ||
<script src="/path/to/fast-format.min.js"></script> | ||
``` | ||
## Browser Support | ||
Chrome, Firefox, Safari, Opera, and Internet Explorer 6+ | ||
Google Chrome, Firefox, Safari, Opera, Microsoft Edge and IE 6+ | ||
## License | ||
MIT | ||
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
6220
51
93
0