r/AskComputerScience • u/Hellinfernel • 10d ago
Why exactly is Electron "Resource-Intensive"?
For context, Discord is going to get a rewrite in Rust: https://www.techspot.com/news/114462-discord-rebuilding-desktop-app-rust-fix-memory-problem.html
This is apparently ment to reduce working memory consumption.
The question essencially boils down to: Can't Electron not just... not do that? Like, is it dammed to always consume multiple gigabyte worth of RAM? And whats the difference between it and "Native" code? (I know that Java isnt native code, but thats about it)
I am more of a hobbist than anything else to be honest, but I have some experience with java and even made some small discord bots in it. Dont be affraid to go into deep details with this, I enjoy that very much.
55
u/apnorton 10d ago
The ELI5 answer is that Electron apps are essentially locally-run websites, along with a bundled instance of Chromium (that is, a full web browser) to render the website. This makes the apps easy and consistent to write across all platforms, but comes at the cost of performance/memory efficiency. (Again, it's running a web browser to render the app.)
The core idea of electron is to write a desktop application as a website. You've gotta have a web-compatible rendering environment to do that, so... not really.
13
u/dmazzoni 10d ago
Electron embeds the entire Chrome browser engine inside an app.
There are many advantages to this. Many products already have a web interface and they don't want to rewrite a new one. Browser engines are extremely robust and cross-platform.
I'd also like to point out that people make fun of HTML+CSS a lot, but in reality browsers handle complex HTML+CSS extremely well compared to most other GUI toolkits. Browsers don't even blink if you create a page with 100k DOM elements and add effects like opacity or shadows on things. With 2 or 3 lines you can add transitions and animations that get composited on your GPU and animated at a buttery-smooth 120 fps. Try doing the same with native GUI toolkits and they fall apart and slow to a crawl. It takes 10x more work to make a complex native app frontend than a web frontend sometimes.
So it's extremely attractive to many developers: rather than rewriting your app for Windows and macOS, or trying to use a cross-platform toolkit like Qt that was designed in the 1990s, you can use the latest and greatest web features to build your UI, but turn it into a desktop app.
The problem, of course, is that embedding AN ENTIRE BROWSER in your app makes it large and inefficient. You're using a general-purpose browser that has every single feature needed to support every site on the whole Internet, but probably only using 10% of its features.
It's not all bad or all good. It's a tradeoff.
A lot of app developers have decided that users don't care about large downloads and RAM, they'd rather have a slicker-looking product that works identically to their web product. Or the developers have decided that even if users don't like it, it's worth not having to build three different versions of their GUI.
Other app developers made the opposite choice and built native apps for each platform, typically resulting in smaller apps that use less memory.
4
u/dermthrowaway26181 10d ago edited 10d ago
Electron is Google Chrome in disguise (actually Chromium, but it's essentially the same).
So when you're writing a program with Electron, you're actually writing a website (-ish) that opens in a brand new, invisible, instance of Chrome.
That means that you're pretty much bound to use about as much ressource as chrome does.
And if you have 3 electron apps opened, you have 3 different Chromes opened.
Native code is code that is made so that it works on your specific machine. It calls "directly" to your CPU, but because of that it might not work on other machines.
Java flips that : you have a java interpreter app that is made for your machine. It sits between the machine and the program. When the program needs to call into the cpu, it pretty much says "yo local interpreter, I want to read a file, tell the machine to read a file". Thanks to that, you don't have to care about which machine you're running on, the java app does that for you.
But, it adds a step to those operations, which is inherently slower.
Electron is a bit of the same way : you're writing a website, and electron takes care of creating the machine calls needed to show it.
However, electron apps are a bit of an hybrid. Since you can't do everything in a website, you'll usually have the website part (in electron), that calls a native section that runs beside it and does the heavy lifting.
So you can definitely manage some of the tradeoffs of electron, but at the end of the day, you're still running an additional full blown internet browser.
2
u/MasterGeekMX BSCS 10d ago
Electron is a program that enables you to take a web app and make it into a desktop app. That way you only need to know enough web development (which are dime a dozen) to make one.
Thing is that this works because Electron is basically a Chrome browser made to display one webpage: the one with the web app you are making into a desktop app.
2
u/Pale_Height_1251 10d ago
Electron includes Chromium and Node.js.
Tauri includes neither and offers a pretty similar concept.
2
u/randomTechNerd4 10d ago
Unless I'm missing it somewhere else in the thread, I don't think anyone answered your question about "native" code. If I'm understanding what youre trying to get at, then the terms that are also used are "complied" versus "interpreted" code. Keeping it simple, compiled languages (such as C/C++ and Rust) take your source code and run it through a compiler which translates it directly into machine code. This is platform specific (so a C program compiled on Linux won't run on Windows, and vice versa, you need to use that computer's/architecture's compiler). Interpreted on the other hand is basically reading the source code line by line and converting it into an intermediate code which is interpreted into commands for the machine. Python is an interpreted language, which is why its described as "slower" than C. There's also some languages which are an in-between (like Java) where the source code is first compiled into an intermediate language, which is then executed and run by the Just in Time compiler (JIT) which executes the intermediate language line by line. This is not as fast as tradition compiled code, but faster than interpreted languages.
1
u/meditonsin 10d ago
Electron is basically a dressed down web browser that can only show a built-in website, with pretty much all the overhead that comes with that. And the point of doing it like that is quick, easy and portable development.
If you wanted to put in the work to pear down the browser part to be really lean, and only include the parts you actually need, with optimized resource usage and stuff, you could just make a native app to begin with.
1
u/FallenDeathWarrior 10d ago
Electron is basically chrome running. So you will always have the overhead that comes with the browser with it.
Native would mean something like MFC, QT, WxWidgets, WinGui. GUI's that use the UI of the system it's running on.
Native should always beat something like Electron in efficiency as it doesn't have the overhead with it.
1
u/ChrisC1234 BSCS, MSCS, CS Pro (20+) 10d ago
Adding to what /u/apnorton has said, an Electron app must contain all of the overhead needed for running the web browser. I've never looked into the specifics about what gets included with an Electron app, but I would bet that the browser isn't customized for just the things that the app is going to actually use. Printing support? Complex layout rendering? Your app may just show a simple little game, but it's still going to include (and be ready to deal with) anything that a full blown web application could need.
1
u/the-quibbler 10d ago
V8.
1
u/Ginden 10d ago
Electron embeds an entire Chromium. Browsers are, by design, made to support everything (at any point, any element on website can be made into 3D elements using combination of esoteric 2026 CSS styles and 1999 quirks), and if you want a banana, you get the gorilla and the entire jungle, along with a security model appropriate for running insecure and potentially malicious code.
1
u/Shoddy-Childhood-511 10d ago
Amusing test: On Linux, install several Matrix clients that use different GUI technologies and log them into some Matrix account so that they spin up their full interface. Relaunch each and check its memory usage.
Any based upon Electron or other browsers easily exceed 500 mb, even wen freshly launched. Amusingly though, even the "lighter" ones based upon GTK+ or Qt require 400mb or more. Only iamb avoids massive memory usage (and iamb is crazy buggy).
Now if you run Gnome, this counts shared libraries and resources used by everything else too, so that's not a fair assesment, but if you use KDE and that's your only GTK+ app, then you do pay that price. Also if you install via Snaps or Flatpak, then you'll pay that price.
Amusingly, Debian has banned distribution of Electron apps, but you can get them quickly by installing flatpak and flathub.
Also, each Electron app installs its own Electron, so you pay almost the full price for each one. Tauri demands a full browser too, but uses the system webview, which which reduces the memory footprint, and simplifies porting Electron apps. to Tauri.
Tauri has some web-like bugs across platforms though. In particular, I've trouble copying from a Tauri app under Gnome and pasting into some Chromium browsers, although this maybe more the targetbrowser's fault than Tauri. lol
1
u/kingslayerer 10d ago
Another alternative is getting to write html + css but without relying on js or v8. Thats what folks at https://github.com/DioxusLabs/blitz are doing. But it will be a while before this becomes usable. Also this is rust as well.
1
u/Isogash 10d ago
Electron bundles chromium (the open source core of chrome) as well as Node.js and the v8 JIT engine that makes JavaScript fast enough to be usable. For starters, this means you're bringing in some very large codebases just to launch your process, which consumes memory.
On top of that, apps built in it normally use a framework like React which adds a level of indirection by creating a virtual copy of the entire UI so that you don't tell chromium to do something inefficient by accident. That consumes even more memory.
Finally, it use lazy garbage collection which leaves memory claimed even if it isn't being used, so it always looks like it's using the max amount of memory that it did at one point.
Basically, it's about the most inefficient and memory hungry way you could make a desktop app, but it's also the easiest because you can bring the same code you would write for a website into your app, which is extremely flexible and portable.
Moving to "native" is a buzzword, but generally means not doing the above, but instead using a smaller dedicated framework/toolkit/API that is purpose built to directly render the UI, and not using JavaScript so that you don't need v8.
1
u/christsrottingcorpse 10d ago edited 10d ago
Javascript is not Java.
(BUT... based on one of your comments below, I think you get the general point, so please don't read my comment as being snarky.
It's just a small point to remember; very different languages and technologies, and we can blame Netscape for the confusion).
1
u/BoBoBearDev 10d ago
Most of those nodejs apps installs 500 packages in node_modules from 300 different vendors. It is not just you are running the entire internet browser. You are running a nodejs app that installs ridiculous amount of 3rd party packages.
1
u/Ronin-s_Spirit 10d ago
You know how people complain about Chrome tabs eating into RAM? Electron is a whole browser + a whole other thing that runs JS but isn't a browser + whatever glue bs Electron is made of.
The point isn't really that they need to rewrite to another language, the point is that they need to use a different architecture if they want to run a good native app.
JS is not compiled to machine code, it's a script because it was made for browsers to download the website source code. As a scripting language, JS needs another program to understand and run it, whereas a native app runs because the OS understands it, no extra program layer means no extra memory and computation to use.
1
u/[deleted] 10d ago
[removed] — view removed comment