this post was submitted on 04 Mar 2025
7 points (100.0% liked)

Explain Like I'm Five

15115 readers
10 users here now

Simplifying Complexity, One Answer at a Time!

Rules

  1. Be respectful and inclusive.
  2. No harassment, hate speech, or trolling.
  3. Engage in constructive discussions.
  4. Share relevant content.
  5. Follow guidelines and moderators' instructions.
  6. Use appropriate language and tone.
  7. Report violations.
  8. Foster a continuous learning environment.

founded 2 years ago
MODERATORS
 

In Kotlin, I can set a bitmap to be 500x500 and use ScaledBitmap to load an image in to it at that size without wasted memory junk as far as I know.

Now I am trying to do this in javascript so I can have my website display it's images to scaled sizes of my choosing without having to load the full size of every image first or manually making and uploading multiple sizes.

Everything I'm seeing says there is no way in JS. My ELI5 here is WHY? What makes Kotlin have this but not JS?

sloplike screenshots for reference

top 5 comments
sorted by: hot top controversial new old
[–] 9point6 13 points 2 days ago (1 children)

JavaScript can do that, but it would be JavaScript running on the server. Any language capable of working with images and http will be able to do this.

If you're running JavaScript in the browser, any kind of operation on the image would require the image to be downloaded from the server first. You can't resize an image without the image after all. Which I think is where you're getting your wires crossed, a Kotlin app already has the file, a browser does not.

One of the main things you need to care about optimising for a web application is sending as little as possible over the network, so you do anything like image resizing either at dev/build-time or on-the-fly server side. There's little point doing it client-side because the comparatively time consuming part of getting it transferred is already done

[–] [email protected] 2 points 2 days ago (1 children)
[–] [email protected] 2 points 2 days ago

If you’re interested Vite is a common build tool for preprocessing a bunch of different files so they can be served efficiently—and there’s plugins for it for image processing where everything is handled automatically

[–] DomeGuy 2 points 2 days ago

Because neither Kotlin nor JavaScript load images.

Kotlin is a Java runtime language, whose most common use is being compiled to bytecode to run full desktop or server applications.

JavaScript is a web browser language, whose most common use is being sent alongside HTML to augment the behavior of a web browser.

Since Kotlin tends to operate outside of a browser sandbox, it makes sense to expose JRE features to allow memory efficient image handling.

In contrast, JavaScript within a browser sandbox only gets images loaded by the web browser, which were already sent over the Internet and loaded in their full size.

( There are ways to run JavaScript outside a browser and Kotlin within, but that's a bigger topic.)

[–] [email protected] 2 points 2 days ago

the sharp library (server side) and the HTML picture element (client side) might be relevant for what you’re trying to accomplish