package
Fast and spec-compliant printf implementation for Node.js and browser.
npm install fast-printfGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Estimated unpacked size across 1 packages.
28.0 kB
Includes dependencies.
Fast and spec-compliant printf implementation for Node.js and browser.
import {
printf,
} from 'fast-printf';
console.log(printf('foo %s', 'bar'));
By default, interpolating an unbound expression produces:
i.e. printf('%s bar') produces %s bar.
This behavior can be overridden by configuring a fast-printf instance using createPrintf:
import {
createPrintf,
} from 'fast-printf';
const printf = createPrintf({
formatUnboundExpression: (
subject: string,
token: PlaceholderToken,
boundValues: any[],
): string => {
console.warn({
boundValues,
position: token.position,
subject,
}, 'referenced unbound value');
return token.placeholder;
};
});
console.log(printf('foo %s'
| implementation | without_placeholders | with_string_placeholder | with_many_string_placeholders |
|---|---|---|---|
sprintf | 31,772,029 | 4,154,748 | 637,229 |
printf | 651,970 | 373,615 | 160,795 |
Results show operations per second (greater is better).
To run the benchmark yourself please see ./benchmark.
// %c character
printf('%c', 'b');
// => 'c'
// %C converts to uppercase character (if not already)
printf('%C', 'b');
// => 'B'
// %d decimal integer (base 10)
printf('%d', 100);
// => '100'
// %0Xd zero-fill for X digits
printf('%05d', 1);
// => '00001'
// %Xd right justify for X digits
printf('%5d', 1);
// => ' 1'
// %-Xd left justify for X digits
printf('%-5d', 1);
fast-printf| 78,068,540 |
| 11,820,632 |
| 2,552,386 |