Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 25th, 2006, 9:14 PM   #11
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Whoops... force of habit, I guess.

What do you mean by "caching the output of those functions"?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 25th, 2006, 9:40 PM   #12
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,024
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
My javascript caches the object to an associative array called "objects". I believe it's faster.
Sane is offline   Reply With Quote
Old May 26th, 2006, 9:40 AM   #13
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Quote:
My javascript caches the object to an associative array called "objects". I believe it's faster.
I'm quite sure it's slower. Lemme write a test now.
Cerulean is offline   Reply With Quote
Old May 26th, 2006, 10:08 AM   #14
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
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>
Cerulean is offline   Reply With Quote
Old May 26th, 2006, 10:59 AM   #15
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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
}
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 26th, 2006, 11:15 AM   #16
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old May 26th, 2006, 11:15 AM   #17
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Well, it's not really the right word for it. It's a reference.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:54 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC