Why not sending JavaScript files in browser-specific bytecode? [closed]

你。 提交于 2019-12-02 20:08:56

问题


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.

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?

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. You could save parsing time at client. Parsing is fast, but it may be worth to do it for low-end devices.
  2. You could put some hints in bytecode. For example, JavaScriptCore (WebKit's JavaScript engine, JSC for short) patches bytecode with information gathered during runtime, such as types. JSC's bytecode is designed in a way that it has slots for such information.

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.


回答1:


  • 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)


来源:https://stackoverflow.com/questions/28649467/why-not-sending-javascript-files-in-browser-specific-bytecode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!