-
Notifications
You must be signed in to change notification settings - Fork 0
โก Bolt: [performance improvement] Replace padStart with inline ternary in date formatters #614
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| const { performance } = require('perf_hooks'); | ||
|
|
||
| function formatDateInput_pad(date) { | ||
| const year = date.getUTCFullYear(); | ||
| const month = String(date.getUTCMonth() + 1).padStart(2, '0'); | ||
| const day = String(date.getUTCDate()).padStart(2, '0'); | ||
| return `${year}-${month}-${day}`; | ||
| } | ||
|
|
||
| function formatDateInput_ternary(date) { | ||
| const year = date.getUTCFullYear(); | ||
| const m = date.getUTCMonth() + 1; | ||
| const d = date.getUTCDate(); | ||
| const month = m < 10 ? '0' + m : m; | ||
| const day = d < 10 ? '0' + d : d; | ||
| return `${year}-${month}-${day}`; | ||
| } | ||
|
|
||
| const dates = Array.from({length: 10000}, () => new Date(Date.now() - Math.random() * 10000000000)); | ||
|
|
||
| let start = performance.now(); | ||
| for(let i=0; i<100; i++) { | ||
| for(const date of dates) { | ||
| formatDateInput_pad(date); | ||
| } | ||
| } | ||
| let end = performance.now(); | ||
| console.log(`padStart: ${end - start}ms`); | ||
|
|
||
| start = performance.now(); | ||
| for(let i=0; i<100; i++) { | ||
| for(const date of dates) { | ||
| formatDateInput_ternary(date); | ||
| } | ||
| } | ||
| end = performance.now(); | ||
| console.log(`ternary: ${end - start}ms`); | ||
|
Comment on lines
+19
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๐ Performance & Scalability | ๐ก Minor | โก Quick win ๐ Supported by static analysis๐ Script executed: #!/bin/bash
set -eu
printf '%s\n' '--- applicable repository conventions ---'
for f in /tmp/coderabbit-repo-knowledge/contextualwisdomlab-scopeweave-647613e1/*/*.md; do
printf '\n### %s\n' "$f"
head -80 "$f"
done
printf '%s\n' '--- perf_test.cjs ---'
cat -n perf_test.cjs
printf '%s\n' '--- tracked status and targeted diff ---'
git status --short
git diff -- perf_test.cjsRepository: ContextualWisdomLab/scopeweave Length of output: 6566 ๋ฒค์น๋งํฌ ์ธก์ ์กฐ๊ฑด์ ๋ณด๊ฐํ์ธ์.
๐ค Prompt for AI Agents |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๐ Info: Ternary yields number type for values >= 10
padStartalways returned a string;m < 10 ? '0' + m : myields a Number when the value is 10 or more. Each formatter wraps the value in a template literal, so the final strings stay identical across the bounded month and day ranges.Was this helpful? React with ๐ or ๐ to provide feedback.