By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Most efficient way to get width of divs inside a container
JavaScript

Javascript – Most efficient way to get width of divs inside a container

Admin
Last updated: 2023/12/20 at 4:34 PM
Admin
Share
3 Min Read
Most efficient way to get width of divs inside a container

Problem:

I have a list of text labels and I need to get a width of those labels when rendered inside the browser. So I’m creating a container element on the fly and append a bunch of divs to it.

Contents
Problem:Solution:
const wrapper = document.createElement('div');
wrapper.style.display = 'flex';
wrapper.style.position = 'absolute';

text.forEach((t) => {
  const d = document.createElement('div');
  d.textContent = t.headerName;
  wrapper.appendChild(d);
});

document.body.prepend(wrapper);

So it’s all rendered into a structure like this:

ff | other | col2andmore |

Now I need to get the width of each element. I do it like this:

const data = Array.from(wrapper.childNodes).map((n) => {
  return {
    width: n.offsetWidth,
  }
});

I’m worried about the performance. I may have tens of thousands of those divs. I know that when reading offsetWidth browser has to run styles recomputes. I’m wondering what’s the most effecient way to get those numbers from divs? I assume that since the container is absolute the recompute will be limited to only this div and its children, not the entire page.

What are your thoughts or suggestions?

Solution:

Doing a quick test here, mapping offsetWidth inside a loop is very fast.

Below is an example, for me it’s about 7ms, the majority of the time is creating the 1000 elements (ps, there are ways to speed that up too).

for (let l = 0; l < 1000; l ++) {
  const s = document.createElement('span');
  s.innerText = `ref:${l} `;
  document.body.appendChild(s);
}

console.time('t1');
const m = [...document.querySelectorAll('span')]
  .map(m => m.offsetWidth);
console.timeEnd('t1');
  
console.log(`size of element 0: ${m[0]}`);
console.log(`size of element 999: ${m[999]}`);

Expand snippet

Profile for the above

In the above profile you can see there are 0 reflow’s during the map. The next layout calc is about 0.34ms after the console.timeEnd and this reflow is only because SO snippets rendering the log.

On the other hand if your dirty the DOM inside the map, you get a massive slowdown. Quick test 1400ms, IOW: 200 times slower.

And below this is what the profile looks like if you dirty the DOM inside the map,. See all those purple blocks, are forced reflows, and the red marks are even Chrome warning it’s a likely performance bottleneck.

Dirty the DOM in Map

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Making a shape a const, and using it multiple times in js Javascript – Making a shape a const, and using it multiple times in js
Next Article How can I get a prompt information and use it inside a function in JavaScript? Javascript – How can I get a prompt information and use it inside a function in JavaScript?
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?