0
\$\begingroup\$

I'm calling the below 2 function once per Cell in a table with over 135 columns and over 200 rows (that is 134*200 = 26800 times, I'm not running the functions on the first column).

I would like to optimize the functions as much as possible. Do you have any suggestions on how to do that, if it even possible?

this.addBodyCellFunction = function(row, index, func, params) {
 var cell = $(document.getElementById("table_row_" + row +"_col_" +index));
 cell.click(function() {
 func.apply(this, params);
 });
 cell.css("cursor", "pointer");
};
this.addBodyCellTooltip = function(row, index, text) {
 var cell = $(document.getElementById("table_row_" + row +"_col_" +index));
 cell.attr("title", text);
};
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Oct 1, 2014 at 14:02
\$\endgroup\$
7
  • 1
    \$\begingroup\$ you could optimize by removing jquery? \$\endgroup\$ Commented Oct 1, 2014 at 14:07
  • 3
    \$\begingroup\$ Use delegation + stylesheet instead of cell.css. \$\endgroup\$ Commented Oct 1, 2014 at 14:07
  • 1
    \$\begingroup\$ Unless you're going to sort the rows and you need to fetch by its original location, you don't need to assign such specific IDs to every column. Just select the table, and use the .rows collection on the table and the cells collection on the row... like: document.getElemntById("my_table").rows[1].cells[3] \$\endgroup\$ Commented Oct 1, 2014 at 14:08
  • 1
    \$\begingroup\$ And in general, ditching jQuery when convenient will greatly improve performance. Like cell.setAttribute("title", text). \$\endgroup\$ Commented Oct 1, 2014 at 14:11
  • \$\begingroup\$ could you post some more source code or better, jsfiddle / jsbin example ? I agree with @Karl-AndréGagnon, cell.css should be done by using css instead of using jquery for this purpose. \$\endgroup\$ Commented Oct 1, 2014 at 14:13

2 Answers 2

1
\$\begingroup\$

There a few options here, for instance you don't need to use Jquery as much as you are using. This will optimize greatly all of your code.
You can simply use JavaScript commands related to table with document.getElementById("table").rows[].cell[]
which is very simple and works very nicely and fast. By doing that there is no need to assign ID's to everything on the table, as you probably are doing now.

answered Oct 1, 2014 at 14:17
\$\endgroup\$
2
  • \$\begingroup\$ Just because the question missuses jQuery, that is no reason to shift to raw Javascript. A delegated event will do a much better job (which is much harder to write in raw javascript). \$\endgroup\$ Commented Oct 1, 2014 at 14:18
  • \$\begingroup\$ Very true. That would be indeed a more elegant approach. \$\endgroup\$ Commented Oct 1, 2014 at 14:28
0
\$\begingroup\$

So, the big one looks like it would switching to one delegate instead of binding a listener to every cell.

Assuming that each $(document.getElementById("table_row_" + row +"_col_" +index)) represents a cell, this should work fine:

$(table).on('td', 'click', function(){func.apply(this, params);})

As to

var cell = $(document.getElementById("table_row_" + row +"_col_" +index));
cell.attr("title", text);

You can get rid of the $ and use plain JS:

// you can also use the document.getElementById("table").rows[].cell[]
// method that Luan suggested.
var cell = document.getElementById("table_row_" + row +"_col_" +index);
cell.setAttribute('title', text)
answered Oct 6, 2014 at 14:59
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.