Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #395 +/- ##
==========================================
- Coverage 86.11% 80.96% -5.15%
==========================================
Files 6 5 -1
Lines 641 825 +184
==========================================
+ Hits 552 668 +116
- Misses 71 123 +52
- Partials 18 34 +16 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
b12412e to
294fef2
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
The code changes introduce a new test, TestModuleDifferentSizes, to validate the loading of JavaScript modules of varying sizes. My review focuses on improving the test's efficiency and maintainability. I suggest using a more efficient method for string building, enabling parallel test execution, and enhancing code clarity by removing commented-out code and magic numbers.
| } | ||
|
|
||
| func TestModuleDifferentSizes(t *testing.T) { | ||
| memoryLimit := uint64(1 * 1024 * 1024) |
| // Uncomment for failures | ||
| // "5k": 5000, | ||
| // "6k": 6000, | ||
| // "7k": 7000, | ||
| // "8k": 8000, |
There was a problem hiding this comment.
| for name, locs := range specs { | ||
| t.Run(name, func(t *testing.T) { | ||
| rt := quickjs.NewRuntime() | ||
| defer rt.Close() | ||
| rt.SetMemoryLimit(memoryLimit) | ||
| ctx := rt.NewContext() | ||
| defer ctx.Close() | ||
|
|
||
| code := "" | ||
| for i := 0; i < locs; i++ { | ||
| code += fmt.Sprintf(`export var myvar%d = "foo%d";`, i, i) + "\n" | ||
| } | ||
|
|
||
| result, err := ctx.LoadModule(code, name+".js") | ||
| require.NoError(t, err) | ||
| defer result.Free() | ||
| }) |
There was a problem hiding this comment.
The sub-tests within this table-driven test can be run in parallel to potentially speed up the test suite execution. To do this safely, you should call t.Parallel() and also capture the range variables (name and locs) within the sub-test's scope to prevent race conditions.
t.Run(name, func(t *testing.T) {
t.Parallel()
// capture range variables to run tests in parallel safely
name := name
locs := locs
rt := quickjs.NewRuntime()
defer rt.Close()
rt.SetMemoryLimit(memoryLimit)
ctx := rt.NewContext()
defer ctx.Close()
code := ""
for i := 0; i < locs; i++ {
code += fmt.Sprintf(`export var myvar%d = \"foo%d\";`, i, i) + "\n"
}
result, err := ctx.LoadModule(code, name+".js")
require.NoError(t, err)
defer result.Free()
})| for i := 0; i < locs; i++ { | ||
| code += fmt.Sprintf(`export var myvar%d = "foo%d";`, i, i) + "\n" | ||
| } |
There was a problem hiding this comment.
Using += to build a string inside a loop is inefficient as it causes multiple memory allocations and copies. For better performance, especially with a large number of lines (locs), it's recommended to use strings.Builder.
var code strings.Builder
for i := 0; i < locs; i++ {
code.WriteString(fmt.Sprintf(`export var myvar%d = \"foo%d\";\n`, i, i))
}
result, err := ctx.LoadModule(code.String(), name+".js")
No description provided.