Skip to content

Format String

Malexion edited this page Jan 13, 2017 · 4 revisions

Format a string template.

Should be familiar to those from .net.

It should be noted that string concatenation, array join and interpolation are still 25% faster than this method, though this figure doesn't really noticeably show until you start using it in a very intensive function.

EX: d3.network graph update link and update node methods are called several times a second for every node and link on display, on large (200+ node 300+ link) charts this would generate noticeable lag with this method vs string concatenation.

Examples

var template = '{0}/{1}/{0}';

console.log(template.format('foo', 'bar'));

console.log('{0}/{1}'.format('foo', 'bar'));