express-fast-json-stringify 1.2.6 → 1.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [1.2.8](https://github.com/nigrosimone/express-fast-json-stringify/compare/v1.2.6...v1.2.8) (2024-09-29)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * global type ([56fa47c](https://github.com/nigrosimone/express-fast-json-stringify/commit/56fa47c6b395a1de89dd85bc67f55c8cd3f5d852))
11
+
5
12
  ### [1.2.6](https://github.com/nigrosimone/express-fast-json-stringify/compare/v1.2.4...v1.2.6) (2024-09-29)
6
13
 
7
14
 
package/README.md CHANGED
@@ -1,59 +1,24 @@
1
- # express-fast-json-stringify
1
+ JSON serialization is a critical task in web development, particularly for applications built with Node.js and Express.js. While the native JSON serialization in Node.js (`JSON.stringify()`) is straightforward and convenient, it can become a performance bottleneck, especially under heavy load. This article introduces [express-fast-json-stringify](https://www.npmjs.com/package/express-fast-json-stringify), a custom middleware package that leverages [fast-json-stringify](https://www.npmjs.com/package/fast-json-stringify) to significantly boost JSON serialization performance in Express applications.
2
2
 
3
- One of the reasons why [Fastify](https://www.npmjs.com/package/fastify) is faster than Express is its use of [fast-json-stringify](https://www.npmjs.com/package/fast-json-stringify). `fast-json-stringify` is a library developed by the Fastify team that boosts JSON conversion speed by analyzing JSON schema definitions.
3
+ ## What is fast-json-stringify?
4
4
 
5
- By using the `fast-json-stringify` library, Fastify can serialize JSON much faster than Express, contributing to its overall performance advantage.
5
+ `fast-json-stringify` is a JSON serialization library developed by the Fastify team. It enhances JSON conversion speed by analyzing JSON schema definitions and compiling them into highly optimized serialization functions. This makes it much faster than the native `JSON.stringify()`, particularly beneficial for high-performance applications.
6
6
 
7
- With `express-fast-json-stringify`, you can leverage `fast-json-stringify` in your Express application as follows:
7
+ ## Introducing express-fast-json-stringify
8
8
 
9
- ```ts
10
- import express from 'express';
11
- import { fastJsonSchema, Schema } from 'express-fast-json-stringify';
9
+ `express-fast-json-stringify` is an npm package that brings the performance benefits of `fast-json-stringify` to Express.js applications. By integrating this package, you can achieve faster JSON serialization, thus improving the overall performance of your application.
12
10
 
13
- const schema: Schema = {
14
- title: 'Example Schema',
15
- type: 'object',
16
- properties: {
17
- firstName: {
18
- type: 'string',
19
- },
20
- lastName: {
21
- type: 'string',
22
- },
23
- age: {
24
- description: 'Age in years',
25
- type: 'integer',
26
- },
27
- },
28
- };
11
+ ## Installation
29
12
 
30
- const app = express();
31
-
32
- app.get('/', fastJsonSchema(schema), (req, res, next) => {
33
- try {
34
- const data = {
35
- firstName: 'Simone',
36
- lastName: 'Nigro',
37
- age: 40,
38
- };
39
- res.fastJson(data);
40
- } catch (error) {
41
- next(error);
42
- }
43
- });
44
- ```
45
-
46
- See the stackblitz [demo](https://stackblitz.com/edit/express-fast-json-stringify).
47
-
48
- ## How to Use
49
-
50
- 1. Install the package:
13
+ First, install the `express-fast-json-stringify` package:
51
14
 
52
15
  ```
53
16
  npm install express-fast-json-stringify
54
17
  ```
55
18
 
56
- 2. Create a schema object that defines the structure of your JSON response, eg.:
19
+ ## Creating a JSON Schema
20
+
21
+ Define a schema object that specifies the structure of your JSON responses. This schema will be used by `fast-json-stringify` to optimize the serialization process.
57
22
 
58
23
  ```ts
59
24
  import { Schema } from 'express-fast-json-stringify';
@@ -62,12 +27,8 @@ const schema: Schema = {
62
27
  title: 'Example Schema',
63
28
  type: 'object',
64
29
  properties: {
65
- firstName: {
66
- type: 'string',
67
- },
68
- lastName: {
69
- type: 'string',
70
- },
30
+ firstName: { type: 'string' },
31
+ lastName: { type: 'string' },
71
32
  age: {
72
33
  description: 'Age in years',
73
34
  type: 'integer',
@@ -76,32 +37,36 @@ const schema: Schema = {
76
37
  };
77
38
  ```
78
39
 
79
- 3. Apply the `fastJsonSchema` middleware to your Express route, passing in the schema object, eg.:
40
+ ## Applying the Middleware
41
+
42
+ Use the `fastJsonSchema` middleware in your Express routes, passing the schema object as an argument. This will set up the optimized JSON serialization for that route.
80
43
 
81
44
  ```ts
45
+ import express, { Request, Response, NextFunction } from 'express';
82
46
  import { fastJsonSchema, Schema } from 'express-fast-json-stringify';
83
47
 
84
- const schema: Schema = {
48
+ const app = express();
49
+
50
+ const exampleSchema: Schema = {
85
51
  title: 'Example Schema',
86
52
  type: 'object',
87
53
  properties: {
88
- firstName: {
89
- type: 'string',
90
- },
91
- lastName: {
92
- type: 'string',
93
- },
94
- age: {
95
- description: 'Age in years',
96
- type: 'integer',
97
- },
54
+ firstName: { type: 'string' },
55
+ lastName: { type: 'string' },
56
+ age: { type: 'integer' },
98
57
  },
99
58
  };
100
59
 
101
- app.get('/', fastJsonSchema(schema), (req, res, next) => {});
60
+ app.get(
61
+ '/',
62
+ fastJsonSchema(exampleSchema),
63
+ (req: Request, res: Response, next: NextFunction) => {},
64
+ );
102
65
  ```
103
66
 
104
- 4. Send JSON Response: Use `res.fastJson()` to send your JSON response, leveraging the speed benefits of `fast-json-stringify`, eg.:
67
+ ## Sending JSON Responses
68
+
69
+ Instead of using the default `res.json()` method, use the `res.fastJson()` method provided by the middleware to send JSON responses. This leverages the speed benefits of fast-json-stringify.
105
70
 
106
71
  ```ts
107
72
  import { fastJsonSchema, Schema } from 'express-fast-json-stringify';
@@ -110,12 +75,8 @@ const schema: Schema = {
110
75
  title: 'Example Schema',
111
76
  type: 'object',
112
77
  properties: {
113
- firstName: {
114
- type: 'string',
115
- },
116
- lastName: {
117
- type: 'string',
118
- },
78
+ firstName: { type: 'string' },
79
+ lastName: { type: 'string' },
119
80
  age: {
120
81
  description: 'Age in years',
121
82
  type: 'integer',
@@ -137,4 +98,16 @@ app.get('/', fastJsonSchema(schema), (req, res, next) => {
137
98
  });
138
99
  ```
139
100
 
140
- By following these steps, you can enhance the performance of your Express applications with faster JSON serialization.
101
+ ## Performance Benefits
102
+
103
+ Using `express-fast-json-stringify` offers several performance benefits:
104
+
105
+ 1. **Increased Speed**: `fast-json-stringify` can serialize JSON data much faster than JSON.stringify(), especially for complex JSON objects.
106
+ 2. **Reduced CPU Usage**: Faster serialization means less CPU time spent on processing, allowing your server to handle more concurrent requests.
107
+ 3. **Consistency and Validation**: By defining JSON schemas, you ensure that the serialized data adheres to a predefined structure, improving data consistency and reducing the likelihood of errors.
108
+
109
+ ## Conclusion
110
+
111
+ Integrating `express-fast-json-stringify` into your Express.js application can provide substantial performance improvements, especially in scenarios where JSON serialization is a bottleneck. By leveraging the power of `fast-json-stringify`, you can achieve faster response times and handle higher loads, making your application more efficient and scalable.
112
+
113
+ To start using `express-fast-json-stringify`, follow the steps outlined in this article, and enjoy the benefits of faster JSON serialization in your Express applications. For a live demo, you can check out the [StackBlitz demo](https://stackblitz.com/edit/express-fast-json-stringify).
@@ -1,10 +1,11 @@
1
1
  import type { NextFunction, Request, Response } from 'express';
2
- import { Options, Schema } from 'fast-json-stringify';
2
+ import { type Options, type Schema } from 'fast-json-stringify';
3
3
  export type { Schema, Options } from 'fast-json-stringify';
4
4
  /**
5
5
  * Set the schema
6
6
  * @param {Schema} schema The schema used to stringify values
7
7
  * @param {Options} options The options to use (optional)
8
+ * @see https://www.npmjs.com/package/fast-json-stringify
8
9
  *
9
10
  * Examples:
10
11
  * ```ts
@@ -44,3 +45,19 @@ export type { Schema, Options } from 'fast-json-stringify';
44
45
  * ```
45
46
  */
46
47
  export declare const fastJsonSchema: (schema: Schema, options?: Options) => (_req: Request, res: Response, next: NextFunction) => void;
48
+ declare global {
49
+ namespace Express {
50
+ interface Response {
51
+ /**
52
+ * Send JSON response.
53
+ *
54
+ * Examples:
55
+ * ```ts
56
+ * res.fastJson({ user: 'tj' });
57
+ * res.status(200).fastJson({ user: 'tj' });
58
+ * ```
59
+ */
60
+ fastJson: (data: any) => Response;
61
+ }
62
+ }
63
+ }
@@ -9,6 +9,7 @@ const fast_json_stringify_1 = __importDefault(require("fast-json-stringify"));
9
9
  * Set the schema
10
10
  * @param {Schema} schema The schema used to stringify values
11
11
  * @param {Options} options The options to use (optional)
12
+ * @see https://www.npmjs.com/package/fast-json-stringify
12
13
  *
13
14
  * Examples:
14
15
  * ```ts
@@ -72,4 +73,4 @@ const fastJsonSchema = (schema, options) => {
72
73
  };
73
74
  };
74
75
  exports.fastJsonSchema = fastJsonSchema;
75
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWlkZGxld2FyZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9saWIvbWlkZGxld2FyZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7QUFDQSw4RUFBZ0U7QUFJaEU7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7O0dBeUNHO0FBQ0ksTUFBTSxjQUFjLEdBQUcsQ0FBQyxNQUFjLEVBQUUsT0FBaUIsRUFBRSxFQUFFO0lBQ2xFLElBQUksQ0FBQyxNQUFNLEVBQUU7UUFDWCxNQUFNLElBQUksU0FBUyxDQUFDLDZDQUE2QyxDQUFDLENBQUM7S0FDcEU7SUFDRCxNQUFNLEdBQUcsR0FBRyxJQUFBLDZCQUFRLEVBQUMsTUFBTSxFQUFFLE9BQU8sQ0FBQyxDQUFDO0lBQ3RDLE9BQU8sQ0FBQyxJQUFhLEVBQUUsR0FBYSxFQUFFLElBQWtCLEVBQUUsRUFBRTtRQUMxRDs7Ozs7Ozs7V0FRRztRQUNILEdBQUcsQ0FBQyxRQUFRLEdBQUcsQ0FBQyxJQUFTLEVBQVksRUFBRTtZQUNyQyxJQUFJLENBQUMsR0FBRyxDQUFDLFNBQVMsQ0FBQyxjQUFjLENBQUMsRUFBRTtnQkFDbEMsR0FBRyxDQUFDLFNBQVMsQ0FBQyxjQUFjLEVBQUUs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FQUFFLGtCQUFrQixDQUFDLENBQUM7YUFDbkQ7WUFDRCxPQUFPLEdBQUcsQ0FBQyxJQUFJLENBQUMsR0FBRyxDQUFDLElBQUksQ0FBQyxDQUFDLENBQUM7UUFDN0IsQ0FBQyxDQUFDO1FBQ0YsSUFBSSxFQUFFLENBQUM7SUFDVCxDQUFDLENBQUM7QUFDSixDQUFDLENBQUM7QUF2QlcsUUFBQSxjQUFjLGtCQXVCekIifQ==
@@ -1,10 +1,11 @@
1
1
  import type { NextFunction, Request, Response } from 'express';
2
- import { Options, Schema } from 'fast-json-stringify';
2
+ import { type Options, type Schema } from 'fast-json-stringify';
3
3
  export type { Schema, Options } from 'fast-json-stringify';
4
4
  /**
5
5
  * Set the schema
6
6
  * @param {Schema} schema The schema used to stringify values
7
7
  * @param {Options} options The options to use (optional)
8
+ * @see https://www.npmjs.com/package/fast-json-stringify
8
9
  *
9
10
  * Examples:
10
11
  * ```ts
@@ -44,3 +45,19 @@ export type { Schema, Options } from 'fast-json-stringify';
44
45
  * ```
45
46
  */
46
47
  export declare const fastJsonSchema: (schema: Schema, options?: Options) => (_req: Request, res: Response, next: NextFunction) => void;
48
+ declare global {
49
+ namespace Express {
50
+ interface Response {
51
+ /**
52
+ * Send JSON response.
53
+ *
54
+ * Examples:
55
+ * ```ts
56
+ * res.fastJson({ user: 'tj' });
57
+ * res.status(200).fastJson({ user: 'tj' });
58
+ * ```
59
+ */
60
+ fastJson: (data: any) => Response;
61
+ }
62
+ }
63
+ }
@@ -3,6 +3,7 @@ import fastJson from 'fast-json-stringify';
3
3
  * Set the schema
4
4
  * @param {Schema} schema The schema used to stringify values
5
5
  * @param {Options} options The options to use (optional)
6
+ * @see https://www.npmjs.com/package/fast-json-stringify
6
7
  *
7
8
  * Examples:
8
9
  * ```ts
@@ -65,4 +66,4 @@ export const fastJsonSchema = (schema, options) => {
65
66
  next();
66
67
  };
67
68
  };
68
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJma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"express-fast-json-stringify",
3
- "version": "1.2.6",
3
+ "version": "1.2.8",
4
4
  "description": "With express-fast-json-stringify, you can leverage fast-json-stringify in your Express application",
5
5
  "main": "build/main/index.js",
6
6
  "typings": "build/main/index.d.ts",