Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: prefer string templates over string concatenation #732

Merged
merged 24 commits into from
Apr 19, 2022

Conversation

pkuczynski
Copy link
Member

String templates are more readable, compact, and a bit faster than string concatenation. Edding eslint rule to force them.

See test results here:
https://jsben.ch/dWhr2

Or benchmark yourself:

const Benchmark = require('benchmark')
const suite = new Benchmark.Suite()
const text = 'some text'
const number = 12

suite.add('concatenation', () => { const output = 'prefix' + text + 'middle' + number + 'postfix' })
suite.add('template', () => {const output = 'prefix' + text + 'middle' + number + 'postfix' })
suite.on('cycle', (event) => { console.log(String(event.target)) })
suite.on('complete', function() { console.log('Fastest is ' + this.filter('fastest').map('name')) })
suite.run({ async: true })

My results in node:

concatenation x 690,777,686 ops/sec ±1.55% (83 runs sampled)
template x 694,885,002 ops/sec ±1.58% (84 runs sampled)
Fastest is template,concatenation

@pkuczynski pkuczynski requested a review from a team as a code owner March 30, 2022 09:22
@pkuczynski pkuczynski self-assigned this Mar 30, 2022
@pkuczynski pkuczynski added the c: chore PR that doesn't affect the runtime behavior label Mar 30, 2022
@pkuczynski pkuczynski added this to the v6.1 - First bugfixes milestone Mar 30, 2022
@pkuczynski pkuczynski mentioned this pull request Mar 30, 2022
@codecov
Copy link

codecov bot commented Mar 30, 2022

Codecov Report

Merging #732 (edcea9e) into main (cb746cb) will decrease coverage by 0.01%.
The diff coverage is 97.56%.

❗ Current head edcea9e differs from pull request most recent head e4f2b75. Consider uploading reports for the commit e4f2b75 to get more accurate results

@@            Coverage Diff             @@
##             main     #732      +/-   ##
==========================================
- Coverage   99.35%   99.34%   -0.02%     
==========================================
  Files        1922     1921       -1     
  Lines      183098   179122    -3976     
  Branches      902      893       -9     
==========================================
- Hits       181924   177945    -3979     
- Misses       1118     1121       +3     
  Partials       56       56              
Impacted Files Coverage Δ
src/name.ts 98.57% <75.00%> (ø)
src/finance.ts 99.29% <93.33%> (-0.02%) ⬇️
src/address.ts 99.81% <100.00%> (ø)
src/commerce.ts 100.00% <100.00%> (ø)
src/datatype.ts 100.00% <100.00%> (ø)
src/fake.ts 100.00% <100.00%> (ø)
src/git.ts 98.30% <100.00%> (ø)
src/helpers.ts 99.27% <100.00%> (ø)
src/image.ts 98.83% <100.00%> (ø)
src/image_providers/lorempicsum.ts 94.44% <100.00%> (ø)
... and 17 more

@pkuczynski
Copy link
Member Author

Needs #733 no to fail...

Copy link
Member

@Shinigami92 Shinigami92 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also update vendor files, we could rename the folder later if you like to

@pkuczynski
Copy link
Member Author

Please also update vendor files, we could rename the folder later if you like to

Check #733 which excludes vendor from eslint... This should be sufficient, right?

@pkuczynski
Copy link
Member Author

Please also update vendor files, we could rename the folder later if you like to

Done...

@pkuczynski pkuczynski requested review from Shinigami92 and a team March 30, 2022 20:44
ST-DDT
ST-DDT previously approved these changes Mar 30, 2022
Copy link
Member

@ST-DDT ST-DDT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The performance gain is minimal and it has some implications on code readability.
Note sure, about it.

@pkuczynski
Copy link
Member Author

The performance gain is minimal and it has some implications on code readability.
Note sure, about it.

The performance gain is minimal, but the readability increases big time. At least to my taste.

@ST-DDT
Copy link
Member

ST-DDT commented Mar 30, 2022

The performance gain is minimal and it has some implications on code readability.
Note sure, about it.

The performance gain is minimal, but the readability increases big time. At least to my taste.

Yes:

-  writeFileSync(resolve(pathOutputDir, methodName + '.md'), content);
+  writeFileSync(resolve(pathOutputDir, `${methodName}.md`), content);

Maybe:

-    return (
-      this.faker.commerce.productAdjective() +
-      ' ' +
-      this.faker.commerce.productMaterial() +
-      ' ' +
-      this.faker.commerce.product()
-    );
+    return `${this.faker.commerce.productAdjective()} ${this.faker.commerce.productMaterial()} ${this.faker.commerce.product()}`;

(We have line breaks for a reason)

Not so much:

-    examples: mdToHtml('```ts\n' + examples + '```'),
+    examples: mdToHtml(`\`\`\`ts\n${examples}\`\`\``),

(Which characters are escaped?)

Not at all:

-    return (
-      this.Helpers.replaceSymbols('???') +
-      this.faker.random.arrayElement(vowels) +
-      this.faker.random.arrayElement(this.ibanLib.iso3166) +
-      this.Helpers.replaceSymbols('?') +
-      '1' +
-      (prob < 10
-        ? this.Helpers.replaceSymbols(
-            '?' + this.faker.random.arrayElement(vowels) + '?'
-          )
-        : prob < 40
-        ? this.Helpers.replaceSymbols('###')
-        : '')
-    );
+    return `${
+      this.Helpers.replaceSymbols('???') +
+      this.faker.random.arrayElement(vowels) +
+      this.faker.random.arrayElement(this.ibanLib.iso3166) +
+      this.Helpers.replaceSymbols('?')
+    }1${
+      prob < 10
+        ? this.Helpers.replaceSymbols(
+            `?${this.faker.random.arrayElement(vowels)}?`
+          )
+        : prob < 40
+        ? this.Helpers.replaceSymbols('###')
+        : ''
+    }`;

(Because now you use nested conditional string templates in combination with string concatenation.)

@ST-DDT ST-DDT added the needs rebase There is a merge conflict label Mar 31, 2022
src/finance.ts Outdated Show resolved Hide resolved
src/commerce.ts Outdated Show resolved Hide resolved
src/address.ts Outdated Show resolved Hide resolved
scripts/verifyCommit.ts Outdated Show resolved Hide resolved
scripts/generateLocales.ts Outdated Show resolved Hide resolved
@ST-DDT ST-DDT self-requested a review April 1, 2022 15:15
@ST-DDT ST-DDT dismissed their stale review April 1, 2022 15:16

See above

@import-brain import-brain removed the needs rebase There is a merge conflict label Apr 3, 2022
@pkuczynski pkuczynski removed the needs rebase There is a merge conflict label Apr 5, 2022
Copy link
Member

@ST-DDT ST-DDT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last quibble, otherwise it looks good to me.

src/internet.ts Outdated Show resolved Hide resolved
# Conflicts:
#	src/finance.ts
#	test/finance_iban.spec.ts
@pkuczynski pkuczynski requested a review from ST-DDT April 6, 2022 08:50
@pkuczynski pkuczynski dismissed ST-DDT’s stale review April 6, 2022 08:50

changes applied

src/internet.ts Outdated Show resolved Hide resolved
ST-DDT
ST-DDT previously approved these changes Apr 6, 2022
@ST-DDT ST-DDT requested a review from a team April 6, 2022 11:47
@ST-DDT ST-DDT added the p: 1-normal Nothing urgent label Apr 6, 2022
@Shinigami92 Shinigami92 added the needs rebase There is a merge conflict label Apr 6, 2022
# Conflicts:
#	src/commerce.ts
#	src/finance.ts
#	src/internet.ts
#	src/lorem.ts
#	src/system.ts
#	src/utils/unique.ts
# Conflicts:
#	src/commerce.ts
#	src/finance.ts
#	src/internet.ts
#	src/lorem.ts
#	src/system.ts
#	src/utils/unique.ts
@pkuczynski
Copy link
Member Author

@ST-DDT @Shinigami92 all changes applied... Can you re-review, please?

@ST-DDT ST-DDT requested a review from a team April 19, 2022 08:28
@ST-DDT ST-DDT removed the needs rebase There is a merge conflict label Apr 19, 2022
@Shinigami92 Shinigami92 merged commit 00b9d4b into main Apr 19, 2022
@Shinigami92 Shinigami92 deleted the string-templates branch April 19, 2022 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: chore PR that doesn't affect the runtime behavior p: 1-normal Nothing urgent
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants