本文介绍了为什么不使用浏览器特定的字节码发送JavaScript文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript没有通用的字节码,但是大多数JavaScript引擎都有自己的字节码.由于JavaScript文件作为源代码字符串传输,因此它们必须在执行之前将源代码字符串解析/编译为字节码.

There is no universal bytecode for JavaScript, but most JavaScript engines have their own bytecode. Since JavaScript files travel as source code string, they have to parse/compile source code string into bytecode at before execution.

但是,由于我们可以在HTTP请求中指定用户代理类型(例如浏览器类型和版本),所以我们不能让服务器为每个浏览器保留字节码并相应地做出响应以节省客户端时间吗?

However, as we can specify a user agent type (e.g. browser type and version) in HTTP request, can't we make the server keep bytecode for each browser and respond accordingly to save some time at client?

是什么使我们无法采用这种方法?我认为,即使某些JavaScript文件以字节码给出,而其他JavaScript文件以源字符串给出,浏览器也不会出现问题.同样,我们在Python中有.pyc文件,并且可以与.py文件一起很好地运行.

What's preventing us from taking this approach? I don't think browsers will have no problem even if some JavaScript files are given in bytecode and others in source string. Similarly, we have .pyc files in Python, and it runs well with .py files.

[更新] 我能想到的潜在好处如下.

[Update]Potential benefits I can think of are below.

  1. 您可以节省客户端的解析时间.解析速度很快,但对于低端设备可能值得这么做.
  2. 您可以在字节码中添加一些提示.例如,JavaScriptCore(WebKit的JavaScript引擎,简称JSC)使用在运行时收集的信息(例如类型)修补字节码. JSC的字节码的设计方式是,它具有用于此类信息的插槽.

就可维护性而言,如果客户端的浏览器不受支持,并且服务器没有太多不同的JavaScript引擎,则服务器始终可以发送原始源代码字符串.在我看来,支持四种最受欢迎​​的浏览器(Chrome,Firefox,IE和Safari)是可行的.此外,我看不到字节码指令集会经常更改.

In terms of maintainability, the server can always send the original source code string if the client's browser is unsupported, and there are not so many different JavaScript engines. Supporting four most popular browsers (Chrome, Firefox, IE and Safari) seems feasible to me. In addition, I don't see bytecode instruction set changing frequently.

推荐答案

  • 所有引擎都需要公开其字节码格式
  • 服务器将需要保存很多不同的字节码文件,甚至需要即时对其进行编译
  • 浏览器检测充满风险(用户代理说谎,代理缓存)
  • 在次要版本的浏览器之间,字节码规则可能会更改
  • 性能提升可能不会那么显着(尤其是与网络传输时间相比)
    • All engines would need to make their byte code format public
    • The server would need to hold very many different byte code files, or even compile them on the fly
    • Browser detection is fraught with peril (user agents lie, proxies cache)
    • Bytecode rules might change between minor versions of a browser
    • The performance gains probably wouldn't be all that significant (especially when compared to network transfer times)
    • 这篇关于为什么不使用浏览器特定的字节码发送JavaScript文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 09:21