function identityTemplateTag(stringsParts, ...values) {
let str = ''
for (let i = 0; i < values.length; i++) str += stringsParts[i] + String(values[i])
return str + stringsParts[stringsParts.length - 1]
}
Plus a built-in option could be more optimized.
This
const result = String.identity`
value: ${someValue}
`
would be essentially be identical to:
const result = `
value: ${someValue}
`
but then it is easy to simply opt into syntax highlighting in certain IDEs:
const html = String.identity
// Various syntax highlighters will now highlight the content of
// the string as HTML code, out of the box:
const result = html`
<div>foo</div>
`
Notice how even ES.Discourse's own syntax highligher highlighted the html content!
The plain-tag one only works on "good-faith" input and doesn't deal with the tag not being used as a tag. But more importantly it uses the wrong coercion semantics too, for example you can't embed a Temporal instance and expect it to be coerced to string, because they have functional toString but throwing valueOf, and addition would call valueOf before toString.
it's been working well for 4 years but yes, it doesn't expect fancy interpolations ... although all its dependent projects never complained about anything, it's supposed to be as minimal and as fast as possible. I will benchmark against the MDN suggested workaround though and update it if that results faster than it is now.
Absolute differences in a micro-benchmark. I see ~50% slower and that may be acceptable, depending on use case. Btw your benchmark.js measures noop twice, and doesn't measure std.