Apr112013
Hmm, that’s a question you probably have NEVER thought about, amirite? Well, after doing a bit of random JavaScript programming yesterday to help out a co-worker, he expressed concern that the solution I implemented (which used recursion) might have problems if the number of iterations was very large. To be honest, I’d never thought about it before; my ignorance stemmed from the fact that I’ve never had to deal with a data set large enough to cause a stack overflow from using recursion.
So, is this a valid concern? Should you stop writing recursive functions? After a quick search on Stack Overflow (oh, the irony), I found this answer, which, while old, gives you a general idea of what to expect with modern browsers.
I threw together a quick jsFiddle that you can run in your favorite browser to find out the limits today. The answer’s author also put together a test which displays results for multiple browsers, so you can see some more up-to-date numbers.
The end result? Unless you’re operating on a data set numbering in the tens of thousands, you don’t have to worry about your recursive algorithm crashing. And the sad part is that my co-worker still wanted me to re-write my code.
Sep202011
By watching television one should be able to guess that no matter how old you get, the same bullshit is always waiting to haunt you… but for whatever reason I reasoned that problems, no matter how complex, are in someway bound and finite. And in that reasoning, I came to the subconscience conclusion that if I work all of the existing ones out, I could actually live in peace… Sadly, this assumption was clearly wrong.
May032011
Me: I didn’t program multitasking support in revolve ball
Me: cos I’m lazy
Ed: you didnt ‘ganbaru’
Me: oh shit! you’re right
Apr212011
OK, so you know about the great jQuery utility method serialize(), which, when applied to a jQuery object that represents a form, will turn all the form data into a key/value serialized string. Randomly, I was asked if it was possible to do the reverse. jQuery doesn’t have a method like that baked in, so here’s my implementation.
function loadSerializedData(formId, data)
{
var tmp = data.split('&'), dataObj = {};
// Bust apart the serialized data string into an obj
for (var i = 0; i < tmp.length; i++)
{
var keyValPair = tmp[i].split('=');
dataObj[keyValPair[0]] = keyValPair[1];
}
// Loop thru form and assign each HTML tag the appropriate value
$('#' + formId + ' :input').each(function(index, element) {
if (dataObj[$(this).attr('name')])
$(this).val(dataObj[$(this).attr('name')]);
});
}
Is there a better way to do this? Let me know in the comments.
Apr072011
function startsWithVowel(word)
{
return [‘a’, ‘e’, ‘i’, ‘o’, ‘u’].indexOf(word[0]) != -1;
}