3

I have more than 20,000 records(rows) in my DOM. Each row has one checkbox and and textbox. When I check any checkbox its corresponding textbox should get disabled that's how logic works.

If Checkbox has id '1' then its corresponding textbox's id is 'text_1',the logic is textbox's id ="text_"+checkbox_id

Scenario : If I have list 10000 id's in random order (of checkboxes), I have to disable their corresponding textbox. My logic below

idList.each(function(id){
        $('#text'+id).attr("disabled",true).val("");
      }
    });

This logic takes around 10-12 sec to disabled all the textboxes.

Is there any way to improve performance.

Suraj Ahirrao
  • 182
  • 10

5 Answers5

2

Try this non-jQuery version:

Array.prototype.slice.call(document.getElementsByTagName('input')).map(function(input){
    if(input.tagName == 'INPUT' && input.type == 'checkbox' && /^text_\d+$/.test(input.id))
    {
        input.disabled = 'disabled';
        input.value = '';
    }
});

It takes around 350ms - 400ms, on my computer, to disable 10000 checkboxes out of 20000 inputs, running Firefox 43.0.4.

Try it here:

//Generate the inputs:
for(var i = 0, html = ''; i < 10000; i++)
 html += '<input type="checkbox" id="text_' + i + '"><input type="text" id="text_' + i + '_">';

document.body.innerHTML += html;


alert('Starting the test');

//Code to be tested comes here
var start = performance.now();

Array.prototype.slice.call(document.getElementsByTagName('input')).map(function(input){
 if(input.tagName == 'INPUT' && input.type == 'checkbox' && /^text_\d+$/.test(input.id))
 {
  input.disabled = 'disabled';
  input.value = '';
 }
});

var end = performance.now();

alert('Time: ' + (end - start) + 'ms');
Ismael Miguel
  • 4,185
  • 1
  • 31
  • 42
1

You can select them by attribute value starting with using the ^= operator:

 $('input[id^=text]').attr("disabled",true).val("");
KAD
  • 10,972
  • 4
  • 31
  • 73
1

Give them a class and select all with that class and add the disabled attribute.

Give the text boxes a class then select them rather than looping

var thisClass = 'textBoxes'; //class for textboxes
$('.'+thisClass).attr('disabled',true);

To test it :

     function disableText(){
        var thisClass = 'textBoxes'; //class for textboxes
          $('.'+thisClass).attr('disabled',true);
    }; 

    var start = +new Date();  // log start timestamp
      console.log(start);

    disableText();//run your change here

    var end =  +new Date();  // log end timestamp
      console.log(end);

    var diff = end - start;
    console.log(diff);//this is the speed it ran at
thatOneGuy
  • 9,977
  • 7
  • 48
  • 90
1

I have made demo with time Difference in different statements for,each and join with id-selector. Check in browser console.

var idList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 91, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9];

console.log("Each Loop");
var start = new Date().getTime();
$(idList).each(function(id) {
  $('#text' + id).attr("disabled", true).val("");
});
console.log((new Date().getTime() - start) +" ms" );

console.log("For Loop");
start = new Date().getTime();
for (var i = 0; i < idList.length; i++) {
  $('#text' + idList[i]).attr("disabled", true).val("");
}
console.log((new Date().getTime() - start) +" ms" );
console.log("Selector");
start = new Date().getTime();
$('#text' + idList.join(', #text')).prop("disabled", true).val("");
console.log((new Date().getTime() - start) +" ms" );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="checkbox" id="text1">
<input type="checkbox" id="text2">
<input type="checkbox" id="text3">
<input type="checkbox" id="text4">
<input type="checkbox" id="text5">
<input type="checkbox" id="text6">
<input type="checkbox" id="text7">
<input type="checkbox" id="text8">
<input type="checkbox" id="text9">
Parth Trivedi
  • 3,802
  • 1
  • 20
  • 40
Ankit Kathiriya
  • 1,221
  • 10
  • 17
0
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>

<script>
    $(document).ready(function () {

        $("#tbl tr").find('td input[type="checkbox"]').change(function () {

            $(this).closest('tr').find('#text_'+$(this).attr("id")+'').attr('disabled','disabled')

        });



    });




</script>
</head>
<body>
 <table style="width:100%" id="tbl">
  <tr>
    <td><input type="checkbox" id="checkbox1"</td>
    <td><input type="text" id="text_checkbox1"</td>
    <td>50</td>
  </tr>
 <tr>
    <td><input type="checkbox" id="checkbox2"</td>
    <td><input type="text" id="text_checkbox2"</td>
    <td>50</td>
  </tr>
 <tr>
    <td><input type="checkbox" id="checkbox3"</td>
    <td><input type="text" id="text_checkbox4"</td>
    <td>50</td>
  </tr>
</table> 


</body>
</html>
Jobelle
  • 2,717
  • 1
  • 15
  • 26