超过 90% 的浏览器能够运行现代 JavaScript,但传统 JavaScript 的流行仍然是当今 Web 性能问题的最大原因之一。

成都创新互联公司主要从事网站制作、网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务隆化,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220
当今的 Web 受到传统 JavaScript 限制,没有任何单一优化可以像使用 ES2017 语法编写、发布和传输网页或软件包那样提高性能。
现代 JavaScript
现代 JavaScript 的特征不是使用特定的 ECMAScript 规范版本编写代码,而是使用所有现代浏览器都支持的语法。Chrome、Edge、Firefox 和 Safari 等现代网络浏览器占据浏览器市场的 90% 以上,依赖相同底层渲染引擎的其他浏览器占另外 5%。这意味着全球 95% 的 Web 流量所来自的浏览器支持过去 10 年来最广泛使用的 JavaScript 语言特性,包括:
- 类 (ES2015)
- 箭头函数 (ES2015)
- 生成器 (ES2015)
- 块范围 (ES2015)
- 解构 (ES2015)
- 剩余和展开参数 (ES2015)
- 对象速记 (ES2015)
- 异步/等待 (ES2017)
较新版本的语言规范中的特性在现代浏览器中获得的支持通常不太一致。例如,许多 ES2020 和 ES2021 特性仅在 70% 的浏览器市场获得支持 — 仍然是大多数浏览器,但还不够安全,不能直接依赖这些特性。这意味着尽管“现代”JavaScript 是一个活动目标,但 ES2017 拥有最广泛的浏览器兼容性,同时包含大多数常用的现代语法特性。换句话说,ES2017 目前最接近现代语法。
传统 JavaScript
传统 JavaScript 是明确避免使用上述所有语言特性的代码。大多数开发人员使用现代语法编写源代码,但将所有内容编译为传统语法以增加浏览器支持。编译为传统语法确实会增加浏览器支持,但效果通常比我们想象的小。在许多情况下,支持度从 95% 左右增加到 98%,但同时产生了大量成本:
- 传统 JavaScript 通常比等效的现代代码大 20% 左右,而且速度更慢。工具缺陷和错误配置通常会进一步扩大这一差距。
- 安装的库占典型生产 JavaScript 代码的 90%。库代码会由于polyfill 和 helper 重复而产生更高的传统 JavaScript 开销,而发布现代代码可以避免这个问题。
npm 上的现代 JavaScript
Node.js 标准化了一个 "exports" 字段来定义软件包的入口点:
{
"exports": "./index.js"
}"exports" 字段引用的模块意味着 Node 版本至少为 12.8,它支持 ES2019。这意味着使用 "exports" 字段引用的任何模块都可以使用现代 JavaScript 编写。软件包使用者必须假定具有 "exports" 字段的模块包含现代代码并在必要时进行转换。
仅现代
如果要发布采用现代代码的软件包,并让使用者在将其用作依赖项时处理转换,则仅使用 "exports" 字段。
{
"name": "foo",
"exports": "./modern.js"
}
不推荐这种方法。在完美的世界中,每个开发人员都已经将编译系统配置为将所有依赖项 (node_modules) 转换为所需语法。但是,目前情况并非如此,仅使用现代语法发布软件包将使其无法在通过旧版浏览器访问的应用程序中使用。
具有传统回退的现代代码
将 "exports" 字段与 "main" 一起使用,以便使用现代代码发布软件包,但还包括用于旧版浏览器的 ES5 + CommonJS 回退。
{
"name": "foo",
"exports": "./modern.js",
"main": "./legacy.cjs"
}
具有传统回退的现代代码和 ESM 捆绑程序优化
除了定义回退 CommonJS 入口点,还可以使用 "module" 字段指向类似的传统回退捆绑包,但该捆绑包使用 JavaScript 模块语法 (import 和 export)。
{
"name": "foo",
"exports": "./modern.js",
"main": "./legacy.cjs",
"module": "./module.js"
}许多捆绑程序(如 webpack 和 Rollup)依赖该字段来利用模块特性和实现摇树优化。这仍然是一个传统捆绑包,不包含除了 import/export 语法之外的任何现代代码,所以使用这种方法来传输具有传统回退、但仍然针对捆绑进行了优化的现代代码。
应用程序中的现代 JavaScript
第三方依赖项构成了 Web 应用程序中绝大多数的典型生产 JavaScript 代码。虽然 npm 依赖项在历史上一直以 ES5 语法的形式发布,但这不再是一个安全假设,并且依赖项更新可能会破坏应用程序的浏览器支持。
随着越来越多的 npm 包转向现代 JavaScript,确保构建工具设置为能够处理它们很重要。您所依赖的一些 npm 包很有可能已经在使用现代语言特性。有许多选择可使用 npm 中的现代代码而不会破坏应用程序在旧版浏览器中的体验,但总体思路是让编译系统将依赖项转换为与源代码相同的目标语法。
webpack
从 webpack 5 开始,现在可以配置 webpack 在生成捆绑包和模块的代码时将使用的语法。这不会转换您的代码或依赖项,只影响由 webpack 生成的“粘附”代码。要指定浏览器支持目标,请在您的项目中添加一个 browserslist 配置,或者直接在 webpack 配置中添加:
module.exports = {
target: ['web', 'es2017'],
};还可以将 webpack 配置为生成优化的捆绑包,当以现代 ES 模块环境为目标时,这些捆绑包会省略不必要的包装函数。这也将 webpack 配置为使用
基本
文件
流程
错误
SQL
调试
- 请求信息 : 2026-02-16 23:05:47 HTTP/1.1 GET : /article/dpphjpd.html
- 运行时间 : 0.0568s ( Load:0.0032s Init:0.0074s Exec:0.0414s Template:0.0048s )
- 吞吐率 : 17.61req/s
- 内存开销 : 2,246.32 kb
- 查询信息 : 12 queries 5 writes
- 文件加载 : 36
- 缓存信息 : 0 gets 2 writes
- 配置加载 : 130
- 会话信息 : SESSION_ID=osfjt7mkf990kfmhas0kjm4lr0
- /home/wwwroot/jxjierui.cn/index.php ( 1.12 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/ThinkPHP.php ( 4.61 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Think.class.php ( 12.26 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Storage.class.php ( 1.37 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Storage/Driver/File.class.php ( 3.52 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Mode/common.php ( 2.82 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Common/functions.php ( 53.56 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Hook.class.php ( 4.01 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/App.class.php ( 13.49 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Dispatcher.class.php ( 14.79 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Route.class.php ( 13.36 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Controller.class.php ( 11.23 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/View.class.php ( 7.59 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/BuildLiteBehavior.class.php ( 3.68 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ParseTemplateBehavior.class.php ( 3.88 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ContentReplaceBehavior.class.php ( 1.91 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Conf/convention.php ( 11.15 KB )
- /home/wwwroot/jxjierui.cn/App/Common/Conf/config.php ( 2.12 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Lang/zh-cn.php ( 2.55 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Conf/debug.php ( 1.48 KB )
- /home/wwwroot/jxjierui.cn/App/Home/Conf/config.php ( 0.32 KB )
- /home/wwwroot/jxjierui.cn/App/Home/Common/function.php ( 3.33 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior.class.php ( 5.62 KB )
- /home/wwwroot/jxjierui.cn/App/Home/Controller/ArticleController.class.php ( 6.11 KB )
- /home/wwwroot/jxjierui.cn/App/Home/Controller/CommController.class.php ( 1.60 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Model.class.php ( 60.11 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php ( 32.43 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db/Driver/Pdo.class.php ( 16.74 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache.class.php ( 3.83 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache/Driver/File.class.php ( 5.87 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Template.class.php ( 28.16 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Template/TagLib/Cx.class.php ( 22.40 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Template/TagLib.class.php ( 9.16 KB )
- /home/wwwroot/jxjierui.cn/App/Runtime/Cache/Home/7540f392f42b28b481b30614275e4e55.php ( 13.96 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/WriteHtmlCacheBehavior.class.php ( 0.97 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ShowPageTraceBehavior.class.php ( 5.24 KB )
- [ app_init ] --START--
- Run Behavior\BuildLiteBehavior [ RunTime:0.000005s ]
- [ app_init ] --END-- [ RunTime:0.000022s ]
- [ app_begin ] --START--
- Run Behavior\ReadHtmlCacheBehavior [ RunTime:0.000142s ]
- [ app_begin ] --END-- [ RunTime:0.000154s ]
- [ view_parse ] --START--
- [ template_filter ] --START--
- Run Behavior\ContentReplaceBehavior [ RunTime:0.000051s ]
- [ template_filter ] --END-- [ RunTime:0.000069s ]
- Run Behavior\ParseTemplateBehavior [ RunTime:0.003732s ]
- [ view_parse ] --END-- [ RunTime:0.003749s ]
- [ view_filter ] --START--
- Run Behavior\WriteHtmlCacheBehavior [ RunTime:0.000087s ]
- [ view_filter ] --END-- [ RunTime:0.000100s ]
- [ app_end ] --START--
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`pid`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( pid= )
- [8] Undefined index: pid /home/wwwroot/jxjierui.cn/App/Home/Controller/ArticleController.class.php 第 47 行.
- [2] file_put_contents(./App/Runtime/Temp/090ba177ab4db98019743d78710ce48d.php): failed to open stream: Permission denied /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache/Driver/File.class.php 第 132 行.
- [8] Undefined index: db_host /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php 第 120 行.
- [8] Undefined index: db_port /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php 第 121 行.
- [8] Undefined index: db_name /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php 第 122 行.
- [2] file_put_contents(./App/Runtime/Temp/cb9926cc206ab759e0c73d1b15d9f394.php): failed to open stream: Permission denied /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache/Driver/File.class.php 第 132 行.

0.0568s
