Template literal `'${string}'` vs JSON.stringify(string)

Benchmark created on


Description

How to wrap string into quotes

Setup

const string = 'test string';

Test runner

Ready to run.

Testing in
TestOps/sec
Template literal
`'${string}'`;
ready
JSON stringify
JSON.stringify(string);
ready
Concatenation
"'" + string + "'";
ready
Concatenation with escaping
'\'' + string + '\'';
ready
Concatenation with toString
"'" + string.toString() + "'";
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.