23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

writing a component won’t necessarily give you a good return on the time you invest; it will be more<br />

productive to just stay in JavaScript. But using a component in a few places where performance really<br />

matters might pay off handsomely.<br />

Tip There is much more to measuring and improving app performance than just offloading<br />

computationally intensive routines to a WinRT component. The Analyzing the performance of Windows<br />

Store apps and Analyzing the code quality of Windows Store apps with Visual Studio code analysis<br />

topics in the documentation will help you make a more thorough assessment of your app.<br />

I also want to add that when I first ran these tests with the example program, I was seeing something<br />

like 100% improvements in C#/C++ over JavaScript. The reason for that had more to do with the nature<br />

of the canvas element’s ImageData object (as returned by the canvas’s createImageData method) than<br />

with JavaScript. In my original JavaScript code (since corrected in Chapter 10 as well), I was<br />

dereferencing the ImageData.data array to set every r, g, b, and a value for each pixel. When I learned<br />

how dreadfully slow that dereference operation actually is, I changed the code to cache the array<br />

reference in another variable and suddenly the JavaScript version became amazingly faster. Indeed,<br />

minimizing identifier references is generally a good practice for better performance in JavaScript. For<br />

more on this and other performance aspects, check out High Performance JavaScript, by Nicholas C.<br />

Zakas (O’Reilly, 2010).<br />

Sidebar: Managed vs. Native<br />

As shown in the previous section, going from JavaScript to C# buys you one level of performance<br />

gain, and going from C# to C++ buys another. Given that C++ is often more complex to work<br />

with, it’s good to ask whether the extra effort will be worth it. In very critical situations where that<br />

extra 13–22% really matters, the answer is clearly yes. But there is another factor to consider: the<br />

difference between the managed environment of .NET languages (along with JavaScript, for that<br />

matter) and the native environment of C++.<br />

Put simply, the reason why C#/VB code is often easier to write than C++ is that the .NET<br />

Common Language Runtime (CLR) provides a host of services like garbage collection so that you<br />

don’t have to worry about every little memory allocation. What this means, however, is that your<br />

activities in C#/VB can also trigger extra processing within that runtime that could change the<br />

performance characteristics of your components.<br />

For example, in the Image Manipulation example with this chapter—which really expanded<br />

into a test app for components!—I added a simple counting function in JavaScript, C#, and C++<br />

(they all look about the same):<br />

function countFromZero(max, increment) {<br />

var sum = 0;<br />

for (var x = 0; x < max; x += increment) {<br />

sum += x;<br />

}<br />

730

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!