1

When I load data in my code-behind, I find that often I want to run several javascript functions based on my data. It it more efficient to use a StringBuilder to compile all of my function calls and issue RegisterStartupScript one time, or is performance the same if I issue RegisterStartupScript everytime I need it?

3 Answers 3

3

I would think that your implementation with a StringBuilder would be a bit more efficient as you are not creating any controls and only register the script once you are finished. The other reason that you may want to stay with your StringBuilder approach is if the order of the registered scripts is important.

From MSDN:

The script block added by the RegisterStartupScript method executes when the page finishes loading but before the page's OnLoad event is raised. The script blocks are not guaranteed to be output in the order they are registered. If the order of the script blocks is important, use a StringBuilder object to gather the scripts together in a single string, and then register them all in a single client script block.

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

1 Comment

I think the bold statement is the key. Thanks!
1

If you are worried about the inefficient reallocation of memory that would occur if you built up the scripts using string concatenation, then don't worry. The scripts that you register are stored in a ListDictionary (System.Collections.Specialized namespace), so the allocation of memory for this list could actually be faster than using a StringBuilder if the StringBuilder weren't instantiated with an appropriate amount of memory. There is also the benefit of not having to build up all your scripts in the one place.

The ListDictionary works like a singly linked list, so while I am not certain, I can't imagine any reason why scripts would not be added to the page in the order they are registered.

Comments

-1

It's generally a lot more efficient to not attempt to solve performance problems before you know you have a performance problem. It's also better to solve the worst performance problems first.

On the other hand, what you're doing is called "preoptimizing". It's not considered a good idea.

Someone else will add the Knuth quote.


Why is PreOptimization a Bad Thing?

Most of us aren't paid to make things the best they can be. We're paid to get a job done.

Sometimes that job requires that we improve the performance of code. At that time, it's best to go find out where the performance requires improvement, as opposed to deciding ahead of time where we thing we want to go look.

"Preoptimizing" decides ahead of time that performance should be improved. Chances are that this does not fix the next worse problem. In fact, without measuring, you could be making things worse and never know.

Even worse, the time you spend fixing the wrong problem could have been spent on fixing the right problem - the one that costs your company money. Chances are that RegisterStartupScript isn't going to cost your company any money, even if you call it for 100 scripts per page. In particular, it won't cost nearly as much as the inefficient implementation or bad design of the scripts themselves.

I don't know if it's true in your case, but in my case, I had a bad case of this disease after two years of college. Fortunately, I ran out of money after two years, and had to go work in the real world. That's one of the things that taught me the difference between theory in school and reality in practice.

A second, more recent thing was spending several days optimizing a memory leak I was told was there. It turns out there was no memory leak, but rather something worse. I had been fixing the wrong problem.


The Knuth Quote

"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil."

From http://en.wikipedia.org/wiki/Optimization_(computer_science)

1 Comment

?? I don't see the problem. I was simply asking if one way of doing something is more efficient than the other. I consider that a lot better than burying my head in the sand and not trying to find better ways of doing things.

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.