![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Whoops... force of habit, I guess.
What do you mean by "caching the output of those functions"? |
|
|
|
|
|
#12 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
My javascript caches the object to an associative array called "objects". I believe it's faster.
|
|
|
|
|
|
#13 | |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#14 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Yup, as I said further up - caching is slower. running with the cached version gives ~23 ms here. Regular document.getElementById gives ~15 ms. This is with Konqueror 3.5. On Firefox, results are less noticeable (cached version ~63 ms, non-cached ~55 ms) but still apparent.
The results really aren't that noticeable but I mean, you waste processing and programmer time by caching the output of getElementById, soo... :banana: Test I used is below: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Caching test</title>
<style type="text/css">
div { padding: 3px; margin: 3px; border: 1px solid #ccc; background-color: #eef }
</style>
<script language="javascript" type="text/javascript">
var cache = new Array();
function cachedElementById(id) {
if(typeof(cache[id]) == "undefined") {
cache[id] = document.getElementById(id);
return cache[id];
}
else return cache[id];
}
function test() {
var startTime = (new Date()).getTime();
// Get reference to a bunch of elements 500 times
var el;
for(var i = 0; i < 100; ++i) {
for(var i = 100; i < 300; ++i) {
// change to test accordingly
// el = document.getElementById("div" + i);
el = cachedElementById("div" + i);
}
}
alert((new Date()).getTime() - startTime + "ms");
}
</script>
</head>
<body onload="test()">
<script language="javascript" type="text/javascript">
// Create a tonne of elements
var body = document.getElementsByTagName("body")[0];
var div = null;
for(var i = 0; i < 5000; ++i) {
div = document.createElement("div");
div.id = "div" + i;
div.appendChild(document.createTextNode("Div " + i));
body.appendChild(div);
}
</script>
</body>
</html> |
|
|
|
|
|
#15 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
If I'm using a single element a lot, I'll "cache" it in a variable.
function DoSomethingCool ()
{
var coolness = document.getElementById("coolness");
// do lots of stuff with "coolness" so I don't have to type the long stuff
} |
|
|
|
|
|
#16 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Me too, but I don't call it "cache", I call it "stash". Glad you put quotes around that
.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|