/    Sign up×
Community /Pin to ProfileBookmark

Keep things in memory vs querySelector

Hello everyone!

I am developing a JavaScript class for a Web app and I would like to ask you for a best practice advice.

There are some images inside a container with id ‘#carousel’.

The script will access a lot of times the child images of the carousel in different JavaScript methods.

Instead of performing a ‘document.querySelector(‘#carousel’).children’ in each of those methods, I decided to perform only one query in the class constructor to keep all the children of ‘#carousel’ in memory. I did something like this:

[code]
function App()
{
let items = document.querySelector(‘#carousel’).items;

function showSlide(slideNumber)
{
items[slideNumber].classList.add(‘show-slide’);
}
}
[/code]

Do you think that keeping all the children images of ‘#carousel’ in memory is a good approach?

Or do you think it would be more efficient to perform a query selector in each method to ask for the carousel images?

Thank you so much!

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@sibertDec 26.2022 — > @jessPHP#1649425 Or do you think it would be more efficient to perform a query selector in each method to ask for the carousel images?

Loading all images in memory may cause an initial delay. Lazy load may reduce the load time.

Loading each image as-you-go may may cause a fraction of this initial delay.

Consider that not everybody use all images.

My vote is to load-as-you-need. Or using lazy load. Or combine.
Copy linkTweet thisAlerts:
@SempervivumDec 26.2022 — When dealing with objects, javascript works reference based: The elements in the nodelist `items` are just references to the image objects in the DOM, no clones or copies. Therefore they do not require a significant amount of space in memory. Therefore there is no harm in handling ths images the way your code does.
Copy linkTweet thisAlerts:
@hebrerilloauthorJan 01.2023 — @Sempervivum#1649436 OK, now I get it, thank you so much!
Copy linkTweet thisAlerts:
@hebrerilloauthorJan 01.2023 — @sibert#1649427 Thank you for your answer. The images of the carousel should all be loaded when the page is first loaded because the carousel is on the top of the page.
Copy linkTweet thisAlerts:
@judekam420Jan 02.2023 — <a href="https://shroomschocolatebars.com/product/gelato-cake-strain/" rel="nofollow ugc">gelato cake strain</a>
Copy linkTweet thisAlerts:
@hebrerilloauthorJan 02.2023 — Hi guys! How can I close this thread as SOLVED?
Copy linkTweet thisAlerts:
@Steve_R_JonesmoderatorJan 02.2023 — {"locked":true}
×

Success!

Help @hebrerillo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...