Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9138317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:14:27+00:00 2026-06-17T09:14:27+00:00

Possible Duplicate: What is better, appending new elements via DOM functions, or appending strings

  • 0

Possible Duplicate:
What is better, appending new elements via DOM functions, or appending strings with HTML tags?

I need to change one thousand <div>‘s — just two attributes. Would it be “cheaper” to remove/add the whole <div>, or just change the attributes? And what would be the cheapest way to do this with Javascript (without leaks, and with as little GC as possible).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:14:27+00:00Added an answer on June 17, 2026 at 9:14 am

    Test it in your target browsers, but given that changing attributes wouldn’t usually require a reflow (obviously depending on what the attributes are, how they affect how styles are applied, etc., etc.) and doesn’t require changing links between DOM nodes, I’d tend to expect changing the attributes to be faster. Replacing the content will require tearing down the old DOM nodes, creating new ones, hooking them into the DOM…

    As for how to do it, it depends a bit on how you identify the divs, but it’s going to be a simple loop regardless. For example, on any modern browser:

    var divs = document.querySelectorAll("selector for the divs");
    var index;
    var div;
    for (index = 0; index < divs.length; ++index) {
        div = divs[index];
        div.setAttribute(/*...*/);
        div.setAttribute(/*...*/);
    }
    

    If the attributes are reflected as properties, you may want to use the reflected property instead to avoid the setAttribute function call. For instance, div.id = "value"; rather than div.setAttribute("id", "value");. But I bet that’s premature optimization.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: A better way to compare Strings which could be null I have
Possible Duplicate: Are PHP short tags acceptable to use? Which is better to use,
Possible Duplicate: For vs Foreach loop in C# Is one better than another? Seems
Possible Duplicate: Use of var keyword in C# Which one is better? using var
Possible Duplicate: ArrayList Vs LinkedList ArrayList vs. LinkedList which one is better for sorting
Possible Duplicate: Inline functions in C++ Modern compilers are better than programmers at deciding
Possible Duplicate: Easiest way to echo HTML in PHP? Hello, one simple and short
Possible Duplicate: C# - Is there a better alternative than this to ‘switch on
Possible Duplicate: C++ STL: Which method of iteration over a STL container is better?
Possible Duplicate: Objective-C: With ARC, what's better? alloc or autorelease initializers? Does ARC automatically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.