In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what is the method of doubling the performance of Node.js services". In the daily operation, I believe that many people have doubts about the method of doubling the performance of Node.js services. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "what is the method of doubling the performance of Node.js services?" Next, please follow the editor to study!
Preface
Students who have developed with Node.js must have been familiar with koa because of its simple and elegant writing, rich community ecology, and many existing Node.js frameworks are re-packaged based on koa. But when it comes to performance, we have to mention a well-known framework: fastify, whose feature is fast by name, and the official Benchmarks is even faster than Node.js 's native http.Server.
Benchmarks
The key to performance improvement
Let's first look at how fastify starts a service.
# install fastify
Npm I-S fastify@3.9.1
/ / create service instance const fastify = require ('fastify') () app.get (' /', {schema: {response: {/ / key is the response status code '200: {type:' object', properties: {hello: {type: 'string'}) Async () = > {return {hello: 'world'}}) / / start the service (async () = > {try {const port = 3001 / / listening port await app.listen (port) console.info (`server listening on ${port} `)} catch (err) {console.error (err) process.exit (1)}) ()
As can be seen from the above code, fastify defines a schema,fastify for the response body of the request, in addition to defining the schema of the response body, it also supports defining schema for the following data:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Body: verifies the request body when it is a POST or PUT method
Query: verify the query parameters of url
Params: verifying url parameters
Response: filter and generate the schema for the response body.
App.post ('/ user/:id', {schema: {params: {type: 'object', properties: {id: {type:' number'}, response: {/ / 2xx indicates that this schema '2xxx: {type:' object' is applicable to the status of 2000299). Properties: {id: {type: 'number'}, name: {type:' string'}, async (req) = > {const id = req.params.id const userInfo = await User.findById (id) / / Content-Type defaults to application/json return userInfo})
The secret to improving the performance of fastify is that instead of using native JSON.stringify when returning application/json type data, it reimplements a set of JSON serialization methods internally, which is the key to doubling the performance of JSON serialization.
How to serialize JSON
Before exploring how fastify serializes JSON data, let's take a look at how tedious steps need to be taken by JSON.stringify. Here we refer to the stringify method implemented in Douglas Crockford (the creator of the JSON format) open source JSON-js.
"JSON-js: https://github.com/douglascrockford/JSON-js/blob/master/json2.js
/ / only show JSON.stringify core code, and other codes omit if (typeof JSON! = = "object") {JSON = {};} JSON.stringify = function (value) {return str (",", ": value})} function str (key, holder) {var value = holder [key]; switch (typeof value) {case" string ": return quote (value); case" number ": return (isFinite (value))? String (value): "null"; case "boolean": case "null": return String (value); case "object": if (! value) {return "null";} partial = []; if (Object.prototype.toString.apply (value) = "[object Array]") {/ / handle array length = value.length; for (I = 0; I
< length; i += 1) { // 每个元素都需要单独处理 partial[i] = str(i, value) || "null"; } // 将 partial 转成 "[...]" v = partial.length === 0 ? "[]" : "[" + partial.join(",") + "]"; return v; } else { // 处理对象 for (k in value) { if (Object.prototype.hasOwnProperty.call(value, k)) { v = str(k, value); if (v) { partial.push(quote(k) + ":" + v); } } } // 将 partial 转成 "{...}" v = partial.length === 0 ? "{}" : "{" + partial.join(",") + "}"; return v; } } } 从上面的代码可以看出,进行 JSON 对象序列化时,需要遍历所有的数组与对象,逐一进行类型的判断,并对所有的 key 加上 "",而且这里还不包括一些特殊字符的 encode 操作。但是,如果有了 schema 之后,这些情况会变得简单很多。fastify 官方将 JSON 的序列化单独成了一个仓库:fast-json-stringify,后期还引入了 ajv 来进行校验,这里为了更容易看懂代码,选择看比较早期的版本:0.1.0,逻辑比较简单,便于理解。 "fast-json-stringify@0.1.0:https://github.com/fastify/fast-json-stringify/blob/v0.1.0/index.js function $Null (i) { return 'null' } function $Number (i) { var num = Number(i) if (isNaN(num)) { return 'null' } else { return String(num) } } function $String (i) { return '"' + i + '"' } function buildObject (schema, code, name) { // 序列化对象 ... } function buildArray (schema, code, name) { // 序列化数组 ... } function build (schema) { var code = ` 'use strict' ${$String.toString()} ${$Number.toString()} ${$Null.toString()} ` var main code = buildObject(schema, code, '$main') code += ` ; return $main ` return (new Function(code))() } module.exports = build fast-json-stringify 对外暴露一个 build 方法,该方法接受一个 schema,返回一个函数($main),用于将 schema 对应的对象进行序列化,具体使用方式如下: const build = require('fast-json-stringify') const stringify = build({ type: 'object', properties: { id: { type: 'number' }, name: { type: 'string' } } }) console.log(stringify) const objString = stringify({ id: 1, name: 'shenfq' }) console.log(objString) // {"id":1,"name":"shenfq"} 经过 build 构造后,返回的序列化方法如下: function $String (i) { return '"' + i + '"' } function $Number (i) { var num = Number(i) if (isNaN(num)) { return 'null' } else { return String(num) } } function $Null (i) { return 'null' } // 序列化方法 function $main (obj) { var json = '{' json += '"id":' json += $Number(obj.id) json += ',' json += '"name":' json += $String(obj.name) json += '}' return json } 可以看到,有 schema 做支撑,序列化的逻辑瞬间变得无比简单,最后得到的 JSON 字符串只保留需要的属性,简洁高效。我们回过头再看看 buildObject 是如何生成 $main 内的代码的: function buildObject (schema, code, name) { // 构造一个函数 code += ` function ${name} (obj) { var json = '{' ` var laterCode = '' // 遍历 schema 的属性 const { properties } = schema Object.keys(properties).forEach((key, i, a) =>{/ / key needs to be enclosed in double quotation marks code + = `json + ='${$String (key)}:'` / / convert value const value = properties [key] const result = nested (laterCode, name, `. ${key}`, value) code + = result.code laterCode = result.laterCode if (I < a.length-1) {code + = 'json + =\' via nested \'}}) code + = `json + ='} 'return json} `code + = laterCode return code} function nested (laterCode, name, key, schema) {var code =' 'var funcName / / determine the type of value Different types are processed differently const type = schema.type switch (type) {case 'null': code + = $Null () `break case' string': code + = `json + = $String (obj$ {key}) `break case 'number': case' integer': code + = `json + = $Number (obj$ {key}) `break case 'object': / / if value is an object A new method is needed to construct funcName = (name + key). Replace (/ [-.\ [\]] / g,') laterCode = buildObject (schema, laterCode, funcName) code + = `json + = ${funcName} (obj$ {key}) `break case 'array': funcName = (name + key). Replace (/ [-.\ [\] / g,') laterCode = buildArray (schema, laterCode) FuncName) code + = `json + = ${funcName} (obj$ {key}) `break default: throw new Error (`${type} unsupported ted`)} return {code, laterCode}}
In fact, it traverses the properties whose type is "object", and then carries out secondary processing for different types of value. If it encounters a new object, it will construct a new function for processing.
/ / if sub-objects are included
Const stringify = build ({
Type: 'object'
Properties: {
Id: {type: 'number'}
Info: {
Type: 'object'
Properties: {
Age: {type: 'number'}
Name: {type: 'string'}
}
}
}
})
Console.log (stringify.toString ())
Function $main (obj) {var json ='{'json + =' "id": 'json + = $Number (obj.id) json + =', 'json + =' "info": 'json + = $maininfo (obj.info) json + ='} 'return json} / / the sub-object handles function $maininfo (obj) {var json =' {'json + =' "age": 'json + = $Number (obj.age) json + =' 'through another function 'json + =' "name": 'json + = $String (obj.name) json + ='} 'return json} so far The study on "what is the method of doubling the performance of Node.js services" is over. I hope to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.