js 序列化_你不知道的Node.js性能优化,读了之后水平直线上升

1、使用最新版本的 Node.js

仅仅是简单的升级 Node.js 版本就可以轻松地获得性能提升,因为几乎任何新版本的 Node.js 都会比老版本性能更好,为什么?

Node.js 每个版本的性能提升主要来自于两个方面:

  • V8 的版本更新;
  • Node.js 内部代码的更新优化。

例如最新的 V8 7.1 中,就优化了某些情形下闭包的逃逸分析,让 Array 的一些方法得到了性能提升:

06dcee76c5e62ef9855ca847395f1bcf.png

Node.js 的内部代码,随着版本的升级,也会有明显的优化,比如下面这个图就是 require 的性能随着 Node.js 版本升级的变化:

776f05d7647631f3dcf5c92fe219bee8.png

每个提交到 Node.js 的 PR 都会在 review 的时候考虑会不会对当前性能造成衰退。同时也有专门的 benchmarking 团队来监控性能变化,你可以在这里看到 Node.js 的每个版本的性能变化。

所以,你可以完全对新版本 Node.js 的性能放心,如果发现了任何在新版本下的性能衰退,欢迎提交一个 issue。

如何选择 Node.js 的版本?

这里就要科普一下 Node.js 的版本策略:

  • Node.js 的版本主要分为 Current 和 LTS;
  • Current 就是当前最新的、依然处于开发中的 Node.js 版本;
  • LTS 就是稳定的、会长期维护的版本;
  • Node.js 每六个月(每年的四月和十月)会发布一次大版本升级,大版本会带来一些不兼容的升级;
  • 每年四月发布的版本(版本号为偶数,如 v10)是 LTS 版本,即长期支持的版本,社区会从发布当年的十月开始,继续维护 18 + 12 个月(Active LTS + Maintaince LTS);
  • 每年十月发布的版本(版本号为奇数,例如现在的 v11)只有 8 个月的维护期。

举个例子,现在(2018年11月),Node.js Current 的版本是 v11,LTS 版本是 v10 和 v8。更老的 v6 处于 Maintenace LTS,从明年四月起就不再维护了。去年十月发布的 v9 版本在今年六月结束了维护。

1547a3d5cedd4d2285520c932957bb2c.png

对于生产环境而言,Node.js 官方推荐使用最新的 LTS 版本,现在是 v10.13.0。


2、使用 fast-json-stringify 加速 JSON 序列化

在 JavaScript 中,生成 JSON 字符串是非常方便的:

const json = JSON.stringify(obj)

但很少人会想到这里竟然也存在性能优化的空间,那就是使用 JSON Schema 来加速序列化。

在 JSON 序列化时,我们需要识别大量的字段类型,比如对于 string 类型,我们就需要在两边加上 ",对于数组类型,我们需要遍历数组,把每个对象序列化后,用 , 隔开,然后在两边加上 [ 和 ],诸如此类等等。

如果已经提前通过 Schema 知道每个字段的类型,那么就不需要遍历、识别字段类型,而可以直接用序列化对应的字段,这就大大减少了计算开销,这就是 fast-json-stringfy 的原理。

根据项目中的跑分,在某些情况下甚至可以比 JSON.stringify 快接近 10 倍!

af2a65955587ee50619dd9c7664defe2.png

一个简单的示例:

const fastJson = require('fast-json-stringify')const stringify = fastJson({ title: 'Example Schema', type: 'object', properties: { name: { type: 'string' }, age: { type: 'integer' }, books: { type: 'array', items: { type: 'string', uniqueItems: true } } }})console.log(stringify({ name: 'Starkwang', age: 23, books: ['C++ Primier', '響け!ユーフォニアム~']}))//=> {"name":"Starkwang
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解释一下这段代码 def add_seq_to_prefix_tree(self, root_node, cluster: LogCluster): token_count = len(cluster.log_template_tokens) token_count_str = str(token_count) if token_count_str not in root_node.key_to_child_node: first_layer_node = Node() root_node.key_to_child_node[token_count_str] = first_layer_node else: first_layer_node = root_node.key_to_child_node[token_count_str] cur_node = first_layer_node if token_count == 0: cur_node.cluster_ids = [cluster.cluster_id] return current_depth = 1 for token in cluster.log_template_tokens: if current_depth >= self.max_node_depth or current_depth >= token_count: new_cluster_ids = [] for cluster_id in cur_node.cluster_ids: if cluster_id in self.id_to_cluster: new_cluster_ids.append(cluster_id) new_cluster_ids.append(cluster.cluster_id) cur_node.cluster_ids = new_cluster_ids break if token not in cur_node.key_to_child_node: if self.parametrize_numeric_tokens and self.has_numbers(token): if self.param_str not in cur_node.key_to_child_node: new_node = Node() cur_node.key_to_child_node[self.param_str] = new_node cur_node = new_node else: cur_node = cur_node.key_to_child_node[self.param_str] else: if self.param_str in cur_node.key_to_child_node: if len(cur_node.key_to_child_node) < self.max_children: new_node = Node() cur_node.key_to_child_node[token] = new_node cur_node = new_node else: cur_node = cur_node.key_to_child_node[self.param_str] else: if len(cur_node.key_to_child_node) + 1 < self.max_children: new_node = Node() cur_node.key_to_child_node[token] = new_node cur_node = new_node elif len(cur_node.key_to_child_node) + 1 == self.max_children: new_node = Node() cur_node.key_to_child_node[self.param_str] = new_node cur_node = new_node else: cur_node = cur_node.key_to_child_node[self.param_str] else: cur_node = cur_node.key_to_child_node[token] current_depth += 1
03-23

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值