fun with JavaScript arrays and string handling
Sunday, November 4th, 2007 12:05 am![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
The support requests status tracker script used to hang at around 300 total requests modified (or once around a hundred requests moved off the board.) Today, it processed "525 updated and 483 moved off this view for a total of 1008 requests changed" in an instant.
The changes I made to the code have worked out much better than I ever planned :-) Of course, the real problem was that earlier versions of the code were complete and utter crap. See, I made a couple of embarrassing rookie mistakes with arrays and string handling.
The very first version of the script used a normal auto-initialized array. To the best of my recollection, the code went something like this:
Storing
The browser promptly hung as it struggled to create a mostly empty 700,000-element array. It only took a couple of refreshes+restarts for me to realize my mistake ;-)
Realized I could use an associative array instead, but I didn't know how to make it recognize the request id as a string instead of automatically converting it to an integer, which would bring us back to the original problem, so I had the bright idea of appending a string to it!
Storing
I then did some substring magic when retrieving the value and comparing. Then, I used string concatenation to print out all the requests that had moved off that page:
Printing Moved Requests
It worked, too. All that string processing made the page lag just a bit (I usually tabbed away from the browser every time I reloaded the Support page), but I thought this was unavoidable, and it really appeased the number-crunching part of me, so I kept it around.
Finally, months after I'd first written the script, I decided to revamp it and realized that:
a.) I could store requests as object properties, which would cut out the string handling for the associative array; and
b.) I could reduce the string concatenation by storing the strings in an array, and then doing a
Printing Moved Requests
Mmmm, I love the smell of optimization in the morning.
I have no idea how to measure how much faster the code changes made it, but my browser no longer hangs, the board no longer takes forever to load even in extreme cases like the one I just put my script through, and I barely notice the time the script takes to process the page. These are all good signs :-)
Note: pseudocode is because I'm too lazy to remember how I actually coded it in old versions. Real code is because I'm too lazy to figure out how to convert my current code to pseudocode.
The changes I made to the code have worked out much better than I ever planned :-) Of course, the real problem was that earlier versions of the code were complete and utter crap. See, I made a couple of embarrassing rookie mistakes with arrays and string handling.
The very first version of the script used a normal auto-initialized array. To the best of my recollection, the code went something like this:
Storing
for each request in currentRequests
oldRequests[request] = request.status
The browser promptly hung as it struggled to create a mostly empty 700,000-element array. It only took a couple of refreshes+restarts for me to realize my mistake ;-)
[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,...,700004, ,,,,,700009,...]
Realized I could use an associative array instead, but I didn't know how to make it recognize the request id as a string instead of automatically converting it to an integer, which would bring us back to the original problem, so I had the bright idea of appending a string to it!
Storing
for each request in currentRequests
oldRequests['id'+request] = request.status
I then did some substring magic when retrieving the value and comparing. Then, I used string concatenation to print out all the requests that had moved off that page:
Printing Moved Requests
for each request in oldRequests
if not in newRequests
removed +=
"<a href='http://www.livejournal.com/support/see_request.bml?id='+request.id+">#"
+ request.id + "</a>"
print removed
It worked, too. All that string processing made the page lag just a bit (I usually tabbed away from the browser every time I reloaded the Support page), but I thought this was unavoidable, and it really appeased the number-crunching part of me, so I kept it around.
Finally, months after I'd first written the script, I decided to revamp it and realized that:
a.) I could store requests as object properties, which would cut out the string handling for the associative array; and
b.) I could reduce the string concatenation by storing the strings in an array, and then doing a
join
on them later:Printing Moved Requests
for each request in oldRequests
if not in newRequests
removed.push(request.id + "'>#" + request.id)
print "<a href='http://www.livejournal.com/support/see_request.bml?id="
+ removed.join("</a> <a href='http://www.livejournal.com/support/see_request.bml?id=")
+ "</a>";
Mmmm, I love the smell of optimization in the morning.
I have no idea how to measure how much faster the code changes made it, but my browser no longer hangs, the board no longer takes forever to load even in extreme cases like the one I just put my script through, and I barely notice the time the script takes to process the page. These are all good signs :-)
Note: pseudocode is because I'm too lazy to remember how I actually coded it in old versions. Real code is because I'm too lazy to figure out how to convert my current code to pseudocode.