Skip to content

Conversation

@cesco69
Copy link
Contributor

@cesco69 cesco69 commented Jan 14, 2026

Optimized array serialization by removing the arrayEnd check inside the hot loop. The serializer now uses a leading-comma strategy instead of checking if (i < arrayEnd) on each iteration.

MAIN

for (let i = 0; i < arrayLength; i++) {
  // ... CODE
  if (i < arrayEnd) {
    json += JSON_STR_COMMA
  }
}

PR

for (let i = 0; i < arrayLength; i++) {
  if (i) {
    json += JSON_STR_COMMA
  }
  // ... CODE
}

This is a micro-optimization FROM (i < arrayEnd) TO (i)

npm run bench:cmp

> [email protected] bench:cmp
> node ./benchmark/bench-cmp-branch.js

Select the branch you want to compare (feature branch):
perf-array-end
Select the branch you want to compare with (main branch):
main
Checking out "perf-array-end"
Execute "npm run bench"

> [email protected] bench
> node --expose-gc ./benchmark/bench.js

short string............................................. x 11,446,586 ops/sec ±1.23% (14420391 runs sampled)
unsafe short string...................................... x 13,957,911 ops/sec ±0.17% (18161063 runs sampled)
short string with double quote........................... x 9,915,924 ops/sec ±0.22% (10426960 runs sampled)
long string without double quotes........................ x 53,899 ops/sec ±0.38% (51201 runs sampled)
unsafe long string without double quotes................. x 13,444,366 ops/sec ±0.11% (17542374 runs sampled)
long string.............................................. x 51,138 ops/sec ±0.29% (49462 runs sampled)
unsafe long string....................................... x 14,171,248 ops/sec ±0.11% (18530093 runs sampled)
number................................................... x 17,419,930 ops/sec ±0.17% (22777816 runs sampled)
integer.................................................. x 14,154,510 ops/sec ±0.11% (18539264 runs sampled)
formatted date-time...................................... x 1,288,798 ops/sec ±0.23% (1246621 runs sampled)
formatted date........................................... x 1,120,467 ops/sec ±0.61% (1075059 runs sampled)
formatted time........................................... x 1,120,802 ops/sec ±0.26% (1078538 runs sampled)
short array of numbers................................... x 71,846 ops/sec ±0.39% (68497 runs sampled)
short array of integers.................................. x 67,728 ops/sec ±0.46% (63886 runs sampled)
short array of short strings............................. x 21,857 ops/sec ±0.49% (20982 runs sampled)
short array of long strings.............................. x 22,721 ops/sec ±0.45% (21887 runs sampled)
short array of objects with properties of different types x 11,080 ops/sec ±0.52% (10782 runs sampled)
object with number property.............................. x 16,842,454 ops/sec ±0.16% (22098035 runs sampled)
object with integer property............................. x 16,837,341 ops/sec ±0.16% (22118005 runs sampled)
object with short string property........................ x 11,581,717 ops/sec ±2.07% (14573414 runs sampled)
object with long string property......................... x 49,656 ops/sec ±0.29% (48120 runs sampled)
object with properties of different types................ x 2,087,554 ops/sec ±0.13% (1942125 runs sampled)
simple object............................................ x 8,138,463 ops/sec ±0.98% (6726046 runs sampled)
simple object with required fields....................... x 8,581,256 ops/sec ±0.92% (7189020 runs sampled)
object with const string property........................ x 14,606,940 ops/sec ±0.12% (19235347 runs sampled)
object with const number property........................ x 16,740,392 ops/sec ±0.12% (21911333 runs sampled)
object with const bool property.......................... x 14,321,953 ops/sec ±0.12% (18769245 runs sampled)
object with const object property........................ x 16,106,706 ops/sec ±0.15% (21117831 runs sampled)
object with const null property.......................... x 13,319,131 ops/sec ±0.19% (17274708 runs sampled)

Checking out "main"
Execute "npm run bench"

> [email protected] bench
> node --expose-gc ./benchmark/bench.js

short string............................................. x 11,955,239 ops/sec ±0.18% (15300593 runs sampled)
unsafe short string...................................... x 16,807,237 ops/sec ±0.13% (21971942 runs sampled)
short string with double quote........................... x 10,198,839 ops/sec ±0.16% (11568114 runs sampled)
long string without double quotes........................ x 60,590 ops/sec ±0.35% (57798 runs sampled)
unsafe long string without double quotes................. x 14,767,797 ops/sec ±1.12% (19396032 runs sampled)
long string.............................................. x 50,590 ops/sec ±0.30% (48860 runs sampled)
unsafe long string....................................... x 17,225,743 ops/sec ±0.13% (22523809 runs sampled)
number................................................... x 16,261,362 ops/sec ±0.13% (21302813 runs sampled)
integer.................................................. x 14,686,924 ops/sec ±0.17% (19158822 runs sampled)
formatted date-time...................................... x 1,284,932 ops/sec ±0.18% (1250203 runs sampled)
formatted date........................................... x 1,124,699 ops/sec ±0.29% (1082105 runs sampled)
formatted time........................................... x 1,120,633 ops/sec ±0.26% (1081227 runs sampled)
short array of numbers................................... x 72,997 ops/sec ±0.46% (68802 runs sampled)
short array of integers.................................. x 72,019 ops/sec ±0.44% (67783 runs sampled)
short array of short strings............................. x 23,295 ops/sec ±0.46% (22374 runs sampled)
short array of long strings.............................. x 23,199 ops/sec ±0.48% (22280 runs sampled)
short array of objects with properties of different types x 11,201 ops/sec ±0.54% (10854 runs sampled)
object with number property.............................. x 14,067,614 ops/sec ±0.15% (18439734 runs sampled)
object with integer property............................. x 14,590,938 ops/sec ±0.15% (19173806 runs sampled)
object with short string property........................ x 11,477,657 ops/sec ±1.21% (14427841 runs sampled)
object with long string property......................... x 51,817 ops/sec ±0.29% (50030 runs sampled)
object with properties of different types................ x 2,126,808 ops/sec ±0.19% (1964332 runs sampled)
simple object............................................ x 7,790,030 ops/sec ±0.78% (6239627 runs sampled)
simple object with required fields....................... x 8,500,411 ops/sec ±0.28% (7069042 runs sampled)
object with const string property........................ x 14,059,843 ops/sec ±0.17% (18318134 runs sampled)
object with const number property........................ x 16,628,530 ops/sec ±0.16% (21715563 runs sampled)
object with const bool property.......................... x 14,262,104 ops/sec ±0.15% (18676180 runs sampled)
object with const object property........................ x 13,390,182 ops/sec ±0.19% (17312968 runs sampled)
object with const null property.......................... x 14,013,509 ops/sec ±0.14% (18278200 runs sampled)

short string..............................................-4.25%
unsafe short string......................................-16.95%
short string with double quote............................-2.77%
long string without double quotes........................-11.04%
unsafe long string without double quotes..................-8.96%
long string...............................................+1.08%
unsafe long string.......................................-17.73%
number....................................................+7.12%
integer...................................................-3.63%
formatted date-time........................................+0.3%
formatted date............................................-0.38%
formatted time............................................+0.02%
short array of numbers....................................-1.58%
short array of integers...................................-5.96%
short array of short strings..............................-6.17%
short array of long strings...............................-2.06%
short array of objects with properties of different types.-1.08%
object with number property..............................+19.73%
object with integer property..............................+15.4%
object with short string property.........................+0.91%
object with long string property..........................-4.17%
object with properties of different types.................-1.85%
simple object.............................................+4.47%
simple object with required fields........................+0.95%
object with const string property.........................+3.89%
object with const number property.........................+0.67%
object with const bool property...........................+0.42%
object with const object property........................+20.29%
object with const null property...........................-4.96%
Back to perf-array-end 1e26d39

@cesco69 cesco69 closed this Jan 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant