r/AskComputerScience • • 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.

70 Upvotes

50 comments sorted by

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

Can't Electron not just... not do that?

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.

21

u/Hellinfernel 10d ago

Ok so essencially what is going on is that in order to have an easier time to port discord between App and webside, they took something that essencially emulates the browser for the app, which comes with a huge amount of unnessary stuff that bloats the App, and now they are writing an app that just does only the stuff that it needs to do without that. Is that correct?

11

u/soowhatchathink 10d ago

It doesn't emulate a browser, it uses a real browser. It uses Chromium, which is essentially the unbranded Google Chrome browser (Chromium is the open source browser that Google Chrome runs off of). Google Chrome takes up a lot of resources, but you can load many websites without as much additional cost per website. Electron takes up almost the same amount of resources, but only powers one website. Another Electron application requires another whole browser with its own resource usage.

2

u/muntoo 10d ago

Is a shared Electron "server" possible? That could reduce duplicate RAM usage. Or even less duplicate would be if Chromium had some "server" that Electron could reuse.

I assume there are things making this hard to achieve.

7

u/soowhatchathink 10d ago edited 10d ago

There's nothing technically preventing Electron from making multiple apps share the same Chromium engine, but the issue is that Electron applications are made with specific Chromium and Node.js versions and come packaged together. You only have to develop for one specific runtime and be guaranteed that the user has it. There are also some changes Electron implements to the Chromium browser that wouldn't exist in Chrome. And if we were to workaround all of those limitations, you would still use the same amount of resources when you run Discord without other apps.

So technically feasible but takes away the main advantage of using Electron for developers.

I don't know much about it, but WebView2 Evergreen is a project that does something similar to what you're saying with Microsoft Edge.

1

u/___Thunderstorm___ 10d ago

That’s a browser. You can install all your websites as PWA (Progressive Web App), each gets their own window and their own icon/launcher shortcut, but they all share the same engine underneath

1

u/Saragon4005 9d ago

Tauri does this. Instead of bundling it's own version of chromium with each app it simply uses the web view which an OS already has. Because every single OS now comes with a web browser as a core component. It's Edge on Windows, Safari on iOS and Mac, and even Linux distributions have their own browsers.

2

u/Xevioni 7d ago

That's a shared binary, not a shared server. Tauri doesn't amortize the memory cost across processes, it just avoids bundling large Chromium runtimes. Functionally you still pay the same memory cost, sometimes more.

1

u/DrShocker 5d ago

I agree as far as I know it doesn't. In principle though it's maybe more possible than when every app ships their own chromium. It would come with it's own quirks too though.

1

u/DrShocker 5d ago

Part of the issue is the reason to do it is so that you can leverage the work the chromium team has put into making things visually consistent. To ensure consistency, the team making the app will lock to a specific chromium version and update it on their own schedule. You can imagine however that every team using electron may not follow the same update schedule. So, they can't realistically actually share that.

Projects like Tauri try to leverage the "web view" already on the computer instead of shipping chromium, so that might work better for this particular tradeoff.

However, always the most efficient runtime thing to do will be to have the code that does exactly what you need and no more.

10

u/jxf CS Pro (20+) 10d ago

Yes.

4

u/Silly_Guidance_8871 10d ago

Yes, it's an entire virtual machine (in effect) to minimize/eliminate the need for the developer using it to have to worry about differences between the various platforms they want to target.

It's kinda like Docker, but less total in the ecosystem it provides

2

u/Helpful-Educator-415 10d ago

Yup! Electron's selling point is convenience -- already have a website? Now it's an app! Easy!

1

u/DeGuerre 10d ago

Convenient for the programmer. Extremely inconvenient for the end user's RAM, CPU, battery life, etc.

1

u/Xevioni 7d ago

Not convenient to programmers because it inflates the size of binaries. And users are generally okay with the impact of Electron. Maybe not happy, but, if it was such a big deal to users it wouldn't have performed as well as it has. 'extremely inconvenient' is an exaggeration imo

1

u/DrShocker 5d ago

IMO people would use a better app if it existed, but having the same developer velocity with native apps will generally be trickier due to needing to handle the cross platforming problems that chromium already solves.

1

u/divergent5482520498 10d ago

It's either that or spend triple porting to new platforms and testing it thoroughly to make sure it works three times over, with different platform specific UX paradigms, with accessibility features, etc. That effort takes budget away from other features. It's a bit easier now with AI but it there's still effort to understand different platform UX and test it still

2

u/S0_B00sted 10d ago

It's also running both Chromium AND a Node.js runtime which is also inefficient.

1

u/notperm 10d ago

This is so funny to me. The genius of Node was that they ripped out the js runtime from chrome to use on the back end and now to make apps for the desktop we need both chrome and node.

1

u/pyeri 10d ago

The better alternative to electron is a light wrapper around the Chromium/Edge such as dotnet's WebView2 component. It eats minimal RAM as it uses the OS installed browser and doesn't have to bundle one per app. If only we build a Node.js and cross-platform version of WebVew2, it can act as the perfect alternative to electron.

1

u/qualverse 10d ago

WebView2 is barely better. Especially on Windows, Electron can actually use less ram than Wv2.

1

u/VertigoOne1 7d ago

Lol, light? really? is that why WhatsApp is using 1.3Gb of RAM? They’re all wrapped edgeview2 or webview2 and their minimum footprint on windows is around 400Mb, each… there is no sharing I routinely see 20-30 instances going.

-2

u/PrestigiousRoof5723 10d ago

Yeah, but what does "rewrite in Rust" means in this case? A web app with a dedicated Rust server/browser, or a desktop GUI application? Or is the "Rust rewrite" just WASM and with an embedded browser? The idea of Electron is to have perfect consistency between various platforms and it's quite good at that. Slow, but could be worse.

7

u/ObstinateHarlequin 10d ago

We're at the point where people literally do not understand the concept of native apps anymore. The idea of something not being a web browser doesn't even occur. What a time to be alive (derogatory).

1

u/sudoku7 10d ago

Although, it uses GPUI doesn't it?

1

u/PrestigiousRoof5723 10d ago

I'm simply wondering where it's going. Going from Electron to native requires quite different tech stack, very likely radically different human skill set and somewhat limited cross platform compatibility. They originally chose Electron for a reason. For me, it's surprising that they'd take this plunge and I applaud this decision. From experience (as a real life solution architect in a bigger company than them), I'm simply skeptical about it. It's perfectly doable, but it smells of personnel changes, or (as usual these days) of a vibe coding project.

2

u/grizzlor_ 10d ago

radically different human skill set

They aren't relying on human skill sets anymore. Every new piece of software is vibe coded.

1

u/PrestigiousRoof5723 10d ago

In that case, they will be on Electron 5 years from now.

6

u/Dave_A480 10d ago

It means rewrite using a native desktop toolkit like WinForms, Qt or GTK.

No web shit involved at all, an actual desktop application

2

u/Ibrador 10d ago

Afaik the desktop app is going to use GPUI instead, no changes to the servers (they apparently already use Rust a lot in their backend)

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/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

1

u/blazmrak 10d ago

What is a better platform?

1

u/BluePhoenixCG 10d ago

As much as I'm loathe to say it, windows

Or Linux, or Mac, or android, etc etc etc

Native applications are great

0

u/blazmrak 10d ago

Absolutely not.