Skip to content
2 changes: 1 addition & 1 deletion frameworks/JavaScript/ultimate-express/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { sjs, attr } from 'slow-json-stringify'
const { DATABASE } = process.env;
const db = DATABASE ? await import(`./database/${DATABASE}.js`) : null;

const jsonSerializer = sjs({ message: attr("string")});
const jsonSerializer = sjs({ message: attr("string") });
Copy link
Contributor

Choose a reason for hiding this comment

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

fast-json-steingify is faster than slow-json-stringify with "unsafe" string type https://www.npmjs.com/package/fast-json-stringify

Copy link
Contributor

Choose a reason for hiding this comment

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

I can't find a benchmark for that

Copy link
Contributor

@nigrosimone nigrosimone Nov 11, 2024

Choose a reason for hiding this comment

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

I have made it here https://stackblitz.com/edit/stackblitz-starters-rlrggo

slow-json-stringify: 13.105ms
fast-json-stringify: string: 0.86ms
fast-json-stringify: unfafe: 0.815ms
JSON.stringify: 0.86ms

for small json JSON.stringify is faster, slow-json-stringify is always slow. Side note "fast-json-stringify: unfafe" is raw fast-json-stringify with no special chars support on string (\n, \r, \t, ", \), Hello, World! no need escape.

Use JSON.stringify, this libraries (fast-json-stringify and slow-json-stringify) works better on complex/huge json

slow-json-stringify last update is 4 years ago (maybe is abbandoned)
fast-json-stringify is active and mantained fy Fastify team

The better option is send as raw/stringified string

app.get('/json', (req, res) => {
  res.setHeader('Server', 'UltimateExpress');
  res.setHeader('Content-Type', 'application/json');
  res.end('{"message":"Hello, World!"}');
});

Copy link
Contributor

Choose a reason for hiding this comment

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

You must serialize JSON in the request as per test rules. Your benchmark has a bug and doesnt serialize object properly, I fixed and tested it and it seems that fast-json-stringify is still fastest, so I switched it over.


const generateRandomNumber = () => Math.floor(Math.random() * maxRows) + 1;

Expand Down