2

This JS will be executed on pages with a lot of fields. Can you see anyway to improve the speed of this code? If so, can you explain what you found?

var _TextInputs = null;
function GetTextInputs()
{
    if (_TextInputs == null)
    {
        _TextInputs = jq('input[type=text]');
    }

    return _TextInputs;
}

var _Spans = null;
function GetSpans()
{
    if (_Spans == null)
    {
        _Spans = jq('span');
    }

    return _Spans;
}


function UpdateRate(ratefield, name)
{
    GetTextInputs().filter('[' + name + ']').each(function()
    {
        this.value = FormatCurrencyAsString(FormatCurrencyAsFloat(ratefield.value));
        CalculateCharge(name.replace('Rate', ''), jq(this).attr(name));
    });
}

function CalculateCharge(name, activity_id)
{
    var inputs = GetTextInputs();
    var bill_field = inputs.filter('[' + name + 'Bill=' + activity_id + ']');
    var rate_field = inputs.filter('[' + name + 'Rate=' + activity_id + ']');
    var charge_field = GetSpans().filter('[' + name + 'Charge=' + activity_id + ']'); 

    charge_field.text(FormatCurrencyAsString(FormatCurrencyAsFloat(bill_field.val()) * FormatCurrencyAsFloat(rate_field.val())));
}
4
  • 4
    Firebug has a great javascript profiler. That will show you where the time is actually being spent. Commented Aug 25, 2009 at 19:49
  • Firefox executes this very quick. The problem is with IE. Commented Aug 25, 2009 at 19:54
  • IE 8 also has a great JavaScript profiler built in. Press F12. Commented Aug 25, 2009 at 21:15
  • @harpo: If you throw up an answer I'll give it to you. Your comment was way more helpful then you probably thought. Commented Aug 25, 2009 at 21:33

3 Answers 3

5

You can:

  • Replace each with while
  • Replace val() with .value (should be fine as long as those fields are plain text ones)
  • Access elements by class instead of by name/type
  • Replace attr() with plain property access; e.g.: this.attr(name) --> this.name

These are all rather unobtrusive changes which should speed things up mainly due to cutting down on function calls.

Don't query elements on every function call if those elements are static (i.e. are not modified during your app life-cycle). Instead, store them outside the loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Between the Firebug profiler and some (in retrospect) obvious temporary variables I did manage to make this code "snappier". Thanks.
4

I can see that you're using attribute filters everywhere, e.g.:

_TextInputs = jq('input[type=text]');

inputs.filter('[' + name + 'Bill=' + activity_id + ']');

Attribute filters are useful, but not especially 'snappy' when compared to more direct class or ID selectors. I can't see any markup so the best I can do is suggest that you use more IDs and classes, e.g.:

jq('input.textInput');

instead of:

jq('input[type=text]');

Comments

0

A little off-topic, but I use and recommend Javascript Rocks. This books contains a TON of awesome JS optimisation advice by the creator of Scriptaculous. Also comes with a tool called DOM Monster which helps track down performance bottlenecks - it's an awesome compliment to Firebug as it actually tracks through the DOM looking for inefficiencies based on heuristics and DOM complexity.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.