I’ve a bad news. TLTR: the benchmark is not reliable I was trying to understand why primitive types performed worse than objects in my PR, but I realized that on primitive types the code of thestringify method is the same (PR and Master), eg, see this benchmark casehttps://github.com/fastify/fast-json-stringify/blob/main/benchmark/bench.js#L44 This schema generate in both case (master and PR), this code: module.exports=functionanonymous(validator,serializer){const{ asString, asNumber, asBoolean, asDateTime, asDate, asTime, asUnsafeString}=serializerconstasInteger=serializer.asInteger.bind(serializer)constJSON_STR_BEGIN_OBJECT='{'constJSON_STR_END_OBJECT='}'constJSON_STR_BEGIN_ARRAY='['constJSON_STR_END_ARRAY=']'constJSON_STR_COMMA=','constJSON_STR_COLONS=':'constJSON_STR_QUOTE='"'constJSON_STR_EMPTY_OBJECT=JSON_STR_BEGIN_OBJECT+JSON_STR_END_OBJECTconstJSON_STR_EMPTY_ARRAY=JSON_STR_BEGIN_ARRAY+JSON_STR_END_ARRAYconstJSON_STR_EMPTY_STRING=JSON_STR_QUOTE+JSON_STR_QUOTEconstJSON_STR_NULL='null'functionmain(input){letjson=''if(typeofinput!=='string'){if(input===null){json+=JSON_STR_EMPTY_STRING}elseif(inputinstanceofDate){json+=JSON_STR_QUOTE+input.toISOString()+JSON_STR_QUOTE}elseif(inputinstanceofRegExp){json+=asString(input.source)}else{json+=asString(input.toString())}}else{json+=asString(input)}returnjson}returnmain}(validator,serializer)The same code had to give the same results... so I decided to benchmarkmaster vs master to see if I got the same results  Checking out "master"Execute "npm run bench"> fast-json-stringify@6.1.1 bench> node ./benchmark/bench.jsshort string............................................. x 25,906,545 ops/sec ±2.29% (183 runs sampled)unsafe short string...................................... x 117,503,246 ops/sec ±5.48% (165 runs sampled)short string with double quote........................... x 16,994,413 ops/sec ±1.85% (187 runs sampled)long string without double quotes........................ x 58,221 ops/sec ±1.35% (193 runs sampled)unsafe long string without double quotes................. x 123,399,562 ops/sec ±5.09% (166 runs sampled)long string.............................................. x 45,787 ops/sec ±2.68% (190 runs sampled)unsafe long string....................................... x 107,531,043 ops/sec ±5.14% (161 runs sampled)number................................................... x 115,566,825 ops/sec ±5.00% (163 runs sampled)integer.................................................. x 99,205,667 ops/sec ±4.17% (170 runs sampled)formatted date-time...................................... x 1,026,106 ops/sec ±3.43% (179 runs sampled)formatted date........................................... x 967,191 ops/sec ±3.49% (184 runs sampled)formatted time........................................... x 1,117,047 ops/sec ±1.31% (193 runs sampled)short array of numbers................................... x 71,463 ops/sec ±1.94% (186 runs sampled)short array of integers.................................. x 63,587 ops/sec ±2.36% (183 runs sampled)short array of short strings............................. x 21,564 ops/sec ±2.08% (188 runs sampled)short array of long strings.............................. x 20,146 ops/sec ±3.08% (176 runs sampled)short array of objects with properties of different types x 10,665 ops/sec ±1.43% (186 runs sampled)object with number property.............................. x 117,962,939 ops/sec ±5.60% (168 runs sampled)object with integer property............................. x 108,928,099 ops/sec ±3.71% (175 runs sampled)object with short string property........................ x 27,080,635 ops/sec ±1.51% (188 runs sampled)object with long string property......................... x 49,784 ops/sec ±1.14% (192 runs sampled)object with properties of different types................ x 1,719,671 ops/sec ±3.24% (183 runs sampled)simple object............................................ x 10,356,645 ops/sec ±0.91% (188 runs sampled)simple object with required fields....................... x 10,100,139 ops/sec ±1.65% (187 runs sampled)object with const string property........................ x 124,866,455 ops/sec ±4.99% (172 runs sampled)object with number property.............................. x 117,962,939 ops/sec ±5.60% (168 runs sampled)object with integer property............................. x 108,928,099 ops/sec ±3.71% (175 runs sampled)object with short string property........................ x 27,080,635 ops/sec ±1.51% (188 runs sampled)object with long string property......................... x 49,784 ops/sec ±1.14% (192 runs sampled)object with properties of different types................ x 1,719,671 ops/sec ±3.24% (183 runs sampled)simple object............................................ x 10,356,645 ops/sec ±0.91% (188 runs sampled)simple object with required fields....................... x 10,100,139 ops/sec ±1.65% (187 runs sampled)object with const string property........................ x 124,866,455 ops/sec ±4.99% (172 runs sampled)object with const number property........................ x 123,480,168 ops/sec ±4.87% (172 runs sampled)object with integer property............................. x 108,928,099 ops/sec ±3.71% (175 runs sampled)object with short string property........................ x 27,080,635 ops/sec ±1.51% (188 runs sampled)object with long string property......................... x 49,784 ops/sec ±1.14% (192 runs sampled)object with properties of different types................ x 1,719,671 ops/sec ±3.24% (183 runs sampled)simple object............................................ x 10,356,645 ops/sec ±0.91% (188 runs sampled)simple object with required fields....................... x 10,100,139 ops/sec ±1.65% (187 runs sampled)object with const string property........................ x 124,866,455 ops/sec ±4.99% (172 runs sampled)object with const number property........................ x 123,480,168 ops/sec ±4.87% (172 runs sampled)object with long string property......................... x 49,784 ops/sec ±1.14% (192 runs sampled)object with properties of different types................ x 1,719,671 ops/sec ±3.24% (183 runs sampled)simple object............................................ x 10,356,645 ops/sec ±0.91% (188 runs sampled)simple object with required fields....................... x 10,100,139 ops/sec ±1.65% (187 runs sampled)object with const string property........................ x 124,866,455 ops/sec ±4.99% (172 runs sampled)object with const number property........................ x 123,480,168 ops/sec ±4.87% (172 runs sampled)simple object............................................ x 10,356,645 ops/sec ±0.91% (188 runs sampled)simple object with required fields....................... x 10,100,139 ops/sec ±1.65% (187 runs sampled)object with const string property........................ x 124,866,455 ops/sec ±4.99% (172 runs sampled)object with const number property........................ x 123,480,168 ops/sec ±4.87% (172 runs sampled)object with const string property........................ x 124,866,455 ops/sec ±4.99% (172 runs sampled)object with const number property........................ x 123,480,168 ops/sec ±4.87% (172 runs sampled)object with const number property........................ x 123,480,168 ops/sec ±4.87% (172 runs sampled)object with const bool property.......................... x 110,656,949 ops/sec ±5.57% (170 runs sampled)object with const object property........................ x 121,682,299 ops/sec ±4.89% (166 runs sampled)object with const null property.......................... x 115,386,130 ops/sec ±4.61% (167 runs sampled)Checking out "master"Execute "npm run bench"> fast-json-stringify@6.1.1 bench> node ./benchmark/bench.jsshort string............................................. x 25,309,923 ops/sec ±2.39% (180 runs sampled)unsafe short string...................................... x 95,582,481 ops/sec ±6.63% (157 runs sampled)short string with double quote........................... x 16,337,301 ops/sec ±2.08% (182 runs sampled)long string without double quotes........................ x 52,829 ops/sec ±2.25% (182 runs sampled)unsafe long string without double quotes................. x 113,528,713 ops/sec ±4.57% (166 runs sampled)long string.............................................. x 46,067 ops/sec ±1.92% (183 runs sampled)unsafe long string....................................... x 107,277,481 ops/sec ±6.01% (152 runs sampled)number................................................... x 107,642,408 ops/sec ±5.38% (159 runs sampled)integer.................................................. x 93,384,162 ops/sec ±4.77% (164 runs sampled)formatted date-time...................................... x 1,125,076 ops/sec ±2.66% (182 runs sampled)formatted date........................................... x 901,179 ops/sec ±2.94% (176 runs sampled)formatted time........................................... x 903,817 ops/sec ±3.39% (182 runs sampled)short array of numbers................................... x 59,918 ops/sec ±3.20% (175 runs sampled)short array of integers.................................. x 54,251 ops/sec ±3.20% (168 runs sampled)short array of short strings............................. x 17,364 ops/sec ±3.48% (173 runs sampled)short array of long strings.............................. x 17,580 ops/sec ±3.18% (174 runs sampled)short array of objects with properties of different types x 8,326 ops/sec ±4.31% (173 runs sampled)object with number property.............................. x 92,969,503 ops/sec ±5.93% (155 runs sampled)object with integer property............................. x 91,242,327 ops/sec ±4.87% (166 runs sampled)object with short string property........................ x 22,060,252 ops/sec ±3.22% (170 runs sampled)object with long string property......................... x 38,606 ops/sec ±3.67% (172 runs sampled)object with properties of different types................ x 1,671,650 ops/sec ±2.98% (172 runs sampled)simple object............................................ x 8,164,079 ops/sec ±3.10% (183 runs sampled)simple object with required fields....................... x 7,888,268 ops/sec ±4.11% (161 runs sampled)object with const string property........................ x 106,484,075 ops/sec ±5.97% (166 runs sampled)object with const number property........................ x 114,081,367 ops/sec ±5.23% (162 runs sampled)object with const bool property.......................... x 105,516,771 ops/sec ±5.75% (156 runs sampled)object with const object property........................ x 101,331,208 ops/sec ±4.76% (161 runs sampled)object with const null property.......................... x 103,993,101 ops/sec ±4.73% (159 runs sampled)short string..............................................+2.36%unsafe short string......................................+22.93%short string with double quote............................+4.02%long string without double quotes........................+10.21%unsafe long string without double quotes..................+8.69%long string...............................................-0.61%unsafe long string........................................+0.24%number....................................................+7.36%integer...................................................+6.23%formatted date-time........................................-8.8%formatted date............................................+7.33%formatted time...........................................+23.59%short array of numbers...................................+19.27%short array of integers..................................+17.21%short array of short strings.............................+24.19%short array of long strings...............................+14.6%short array of objects with properties of different types+28.09%object with number property..............................+26.88%object with integer property.............................+19.38%object with short string property........................+22.76%object with long string property.........................+28.95%object with properties of different types.................+2.87%simple object............................................+26.86%simple object with required fields.......................+28.04%object with const string property........................+17.26%object with const number property.........................+8.24%object with const bool property...........................+4.87%object with const object property........................+20.08%object with const null property..........................+10.96%Back to master 2b37f1d
instead, even if I compared the same branch, I got the result that the second run was better than the first one! I have the impression that the garbage collector give an advantage to the second branch to be executed, We need a more reliable benchmark to accept PR on performance like this one (see#803). |
Uh oh!
There was an error while loading.Please reload this page.
It'a a good performance improvments on Array and Object, but not on primitive types
Raw benchmark