While there is no universal bytecode for JavaScript, most JavaScript engines have their own bytecode. The source code string of JavaScript files must be parsed/compiled into bytecode before execution.
Could servers store bytecode for different browsers and respond accordingly based on the user agent type specified in HTTP requests? This could potentially save time at the client end.
What obstacles are preventing this approach? It seems like browsers should be able to handle receiving JavaScript files in both bytecode and source string formats without issues. Python has .pyc files that work alongside .py files, so why not something similar for JavaScript?
[Update] Some potential benefits of storing bytecode include:
- Saving parsing time at the client, especially beneficial for low-end devices where parsing may take longer.
- Adding hints in bytecode, such as types information patched by JavaScriptCore (JSC) during runtime. JSC's bytecode allows slots for additional information.
In terms of maintainability, servers can still send original source code strings if the client browser is unsupported since there are only a few major JavaScript engines (e.g., Chrome, Firefox, IE, Safari). Additionally, bytecode instruction sets do not change frequently.