Introduction
I have noticed an interesting anomaly between Pyodide and Pyscript. Pyscript takes about one second longer to load an HTML + Python page than Pyodide. Pyodide averages 2 seconds and Pyscript 3 seconds.
Other interesting stats:
Downloads:
- Pyodide downloads 8.0 MB compressed – 18.0 MB uncompressed
- Pyscript downloads 8.7 MB compressed – 22.7 MB uncompressed.
Load Times:
- Pyodide loads in 558 ms on average and finishes in 2 seconds on average.
- Pyscript loads in 2900 ms on average and finishes in 3 seconds on average.
Pyscript load seven additional files:
- pyscript.js
- pyscript.css
- livereload.js
- packages.json
- micropip-0.1-py3-none-any.whl
- pyparsing-3.0.7-py3-none-any.whl
- packaging-21.3-py3-none-any.whl
The file packages.json lists the supported Python packages. Knowing the content of this file is important.
One item I discovered from packages.json is that Pyscript/Pyodide is WASM32 (32-bit) and not WASM64. This will be an important item for AI/ML/Data Science as WASM32 in the browser might only support 2 GB of memory. I need to test this as there is work being done in Chrome to support 4 GB of memory link. I had incorrectly assumed that 32-bit anything was no longer being designed for new architectures.
Demo Examples
Start your browser, enable the debugger, go to the Network tab, and load these pages. These pages will show the page load times for Pyodide and Pyscript.
Notice the delay that happens on the second link during the load of distutils.tar. There is something delaying that step. I do not know why yet. The key is that Pyodide is faster than Pyscript. However, Pyscript is offering additional features.
I am including the code for these examples.
Pyodide Page Load Time Example:
This example also demonstrates how to call a Python function from JavaScript and how to access a JavaScript variable from Python.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<!DOCTYPE html> <html> <head> <title>Pyodide Load Time</title> <script src="https://cdn.jsdelivr.net/pyodide/v0.20.0/full/pyodide.js"></script> </head> <body> <h4>Example program measuring Pyodide load time</h4> <div id="msg">Loading page ...</div> <br> <jhanley-python> import time import js from js import document def pymain(): now = time.time_ns() / 1000000 start = js.start_time delta = (now - start) / 1000 document.getElementById("msg").innerHTML = 'Total start time: ' + str(delta) + ' seconds' </jhanley-python> <script type="text/javascript"> async function main() { // Hide the tag contents var tag = document.getElementsByTagName('jhanley-python'); tag[0].style.display = "none"; let pycode = tag[0].innerHTML; let pyodide = await loadPyodide(); pyodide.runPython(pycode); // Call the Python main() function pyodide.globals.get("pymain")() } var start_time = (new Date()).getTime(); main(); </script> </body> </html> |
Pyscript Page Load Time Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<!DOCTYPE html> <html> <head> <title>Pyscript Load TimePy</title> <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /> <script defer src="https://pyscript.net/alpha/pyscript.js"></script> </head> <body> <h4>Example program measuring Pyscript load time</h4> <div id="msg">Loading page ...</div> <br> <py-script> import time import js from js import document def main(): now = time.time_ns() / 1000000 start = js.start_time delta = (now - start) / 1000 document.getElementById("msg").innerHTML = 'Total start time: ' + str(delta) + ' seconds' main() </py-script> <script type="text/javascript"> var start_time = (new Date()).getTime(); </script> </body> </html> |
Summary
Currently, Pyscript is in alpha status and Pyodide is at version 0.20. That means neither one is production. My objective in analyzing the page start times is to find ways to improve this. One technique I am reviewing is the Virtual File System. By switching the file system to IDBFS, which is backed by IndexedDB, Pyscript and Pyodide files can be saved for faster page loading for the same domain. I will publish another article on the Virtual File System.
More Information
Photography Credit
I write free articles about technology. Recently, I learned about Pexels.com which provides free images. The image in this article is courtesy of Pixabay at Pexels.
I design software for enterprise-class systems and data centers. My background is 30+ years in storage (SCSI, FC, iSCSI, disk arrays, imaging) virtualization. 20+ years in identity, security, and forensics.
For the past 14+ years, I have been working in the cloud (AWS, Azure, Google, Alibaba, IBM, Oracle) designing hybrid and multi-cloud software solutions. I am an MVP/GDE with several.
Leave a Reply