Category Archives: JavaScript

So I mentioned something on my interview with Alex of NativeScripting.com that he did with me. And someone asked about this in the comments, so I decided to create a blog article on this specific optimization tip.

I am going to code this to a browser rather than in NativeScript because well JS in a browser is just plain easier to show the memory issues because you can plop the code in to a new browser tab and step through the code instantly. 🙂

These lines are fine, we allocated colors, then looped through them. So far so good. Lets proceed, what about:

const labels=["Wow","Awesome","Interview", "Alex", "Created!"];

Well here is the first issue; Fortunately for us, the v8 JS engine actually pre-allocates the 5 different string "Wow" through "Created!" when it parses the JavaScript however, each loop through it does create a brand new Labels array and then links those strings into it. So in this case it is small overhead; however in the 3 loops that still is 2 more sets of allocations and all new GC enties that have to be cleaned up after the loop is completed. If you were to put this single line of code outside the loop; your code runs the same, but no less memory usage and less processing.

const clr = new Color(<strong><em>colors</em></strong>[i]);

What about this line, well in this case all 15 times it creates a brand new Color object. You can easily make this only be ran 3 times just by moving it outside the inner for (j) loop into the for (i) loop. In this example the Color class is very light weight. But many classes you create inside loops will be very heavy with lots of allocations going on when you allocate the class. So pay attention, 3 allocations vs 15 allocations, all by putting the line in the wrong spot...

Continuing on, most the rest of the lines has to be inside the loop; but I tossed another item into the loop, that wasn't needed... This line is very easy to accidentally end up in the loop because you needed it at that point.

Yep, this has to do an expensive lookup in the dom to find this element, it never changes. It also has to allocate it to that "parent" variable 15 times. If you dropped it before/outside of both loops, you would have a single lookup and allocation...

---

Finally lets look at the last example; it suffers from the same issues. However, this one I used a Array.forEach rather than the simple for (i) loop. Why do I say it can be worse then? Well in small numbers, the difference is actually pretty tiny. The extra amount of memory it uses over a for (i) loop is also reasonable. But in larger loops (especially loops inside of loops inside of loops); the difference can end up being exponentially larger.

Your creating and calling functions, and all those functions now has added scopes and the engine has to setup all the callstacks for each and every iteration, additional error checking and just plain running a lot more code behind the scenes. So what is a simple for (i) loop in the first example, now became 15 EXTRA functions calls with all the extra overhead of cpu and memory that entails. Again in small chunks they are perfectly fine, but if you are wanting performance, every ms and gc adds up and sometimes they can add up very quickly when dealing with the Array helper functions that use callbacks...

As a better way to phrase this; imagine you have a for (i) loop that takes 5ms and a forEach loop takes 25ms to finish. Both are still dang fast, hard to even benchmark. But now you embed it in another forEach that also by itself takes 25ms. 25ms * 25ms = 625ms. More than half of a second. But, if you were to switch this to a for (i) loop each of them is 5ms. So, 5ms * 5ms = 25ms. Both are exponentially larger, but 25ms vs 625ms and the difference starts becoming much clearer. Now add a third loop; 5ms*5ms*5ms = 125ms. 25ms*25ms*25ms=15,625ms (or > 15 seconds!) In small chunks the time and memory used from a forEach is minuscule. But depending on how many times it is called; it can add up very quickly to be a major difference.

Please note this goes for all the nice Array helper functions, .map, .reduce, .filter, etc. Everyone of them that uses a callback, has somewhere between 2 times and 50 times as much overhead as a simple for (i) loop. Remember, despite this overhead; these functions are extremely fast. So don't be afraid to use them, but be more wary of using them in deeply nested code that can easily be called in loops when the datasets they are working against are also large.

As many items that you can actually move outside of your loop, the better off you are. This Includes any expensive calculations! You might be able to do 99% of the expensive calculation outside of the loop; and then finish off the last part of the calculation inside the loop...

I hope this answers the question about loops and performance. Every loop is a golden opportunity to greatly enhance your app,

The next major version of NativeScript (version 6) will be switching from the android.support packages to the new androidx namespace. This will be a hard break; as you can only use the support api's or androidx api's; but not both. This is something Google has been implementing for a while; and NativeScript is getting on board to eliminate later issues and give us great support. The awesome developers at Progress have already created a fork of NativeScript; which you can install to test which uses androidx instead of using the support libraries.

Since one of my plugins is affected by the changes; I took a look into what was required to support both the current versions of NativeScript and the new upcoming version of NativeScript.

For any of you who have plugins; this code is what I basically devised.

Then you can use androidSupport to access the namespace, and it pretty much works the same as what you would access using android.support.v4 or the new androidx.core replacement. Because we used feature detection; this allows us to support both namespaces in the same code base.

So if you have JavaScript or TypeScript code that uses android.support.* then you can use this technique to make your plugin forwards compatible with the next major version of NativeScript while retaining support for the prior versions.

For those who upgraded to 5.0.0 last month, it was a bit of a rough ride. A lot of cool features but a lot of weird corner case broken items. Fortunately since that point, they have released a several 5.0.x point releases which fixed several of the larger flaws. 5.1.0 actually fixes several of the non-critical smaller flaws and adds some cool new features... So if you were waiting to jump on the 5.x bandwagon, this should be the stable release you are waiting for!

The quick list of some of the new features in 5.1

Core Modules

Enable modal dialog chaining in IOS - this allows you to have another dialog follow the first; anyone who has tried this in the past; know this was always a pain on iOS.

isScrollEnabled - This allows you to disable scrolling in the scrollbar component.

androidSwipeEnabled - Allows you to disable swiping in the Android tabview control.

You can use the arrow keys to select an option, in this screen shot; the "Plain JavaScript" is chosen and so it is highlighted.

Android

Android AAB (Android App Bundle); support added!

New package.json flag; suppressCallJSMethodExceptions: true/false - enables suppression of the boolean errors when calling a native function. This could happen and crash the app. Now you can suppress them. (see http://fluentreports.com/blog/?p=581 for more information)

extends should now work against standard javascript classes (i.e. non native Android classes); so that you can now do class MyVue extends Vue {}; class blahComp extends MyView {}; and it will work properly.

iOS

CStrings are a bit more resilient when passing to a function that wants them as a pointer to read and possibly write.

CLI

Updating NativeScript

To get updated; you first need to do:npm i -g nativescript@latest

That will get you the latest version of NativeScript CLI; once you have it; do a "tns --version" and verify it prints out "5.1.x". Then do a "tns doctor" to verify your environment is up to date and has all the newest support tools you need for a successful build.

To install Webpack & HMR support:npm i nativescript-dev-webpack@latest --save-devNote: you need to have nativescript-dev-webpack as a development dependency for HMR to work.

To install latest NativeScript Angular pluginnpm i nativescript-angular@latest --saveYou will then need to install the actual angular bits; which as of this post v6 is currently supported.

The addition of all the additional analytics/tracking to the CLI reminded me; you can disable it permanently; if you value your privacy by doing:tns usage-reporting disable && tns error-reporting disable

Known issues

App Resume on Android can still crash in some situations. (Better than in 5.0)

It has been a long road getting to this point; but I'm happy to see that the NativeScript team released The final piece of the 5.0. The CLI tool which was the final piece was finally released on Nov 2nd; the rest of the pieces have been released for a couple days.

5.0 adds a whole lot of new awesome features; however since several pieces have changed; I would highly recommend before uploading a 5.0 app to the app stores you do another full test of your application; testing everything as several low level framework items changed for both iOS and Android.

The quick list of some of the new features in 5.0

CLI Prompts

The cli now will prompt you for input; so when you type "tns create ProjectName" you will see something like this:

You can use the arrow keys to select an option, in this screen shot; the "Plain JavaScript" is chosen and so it is highlighted.

HMR - Hot Module Replacement

This is the feature most NativeScript developers have been waiting for; this is awesome. You know the silly change a file app restarts; change another file app restarts livesync? Now you can do this: tns run --hmr

HMR, allows the page and JavaScript code to be updated dynamically (no more full app restarts) ; if you change a class the class may have to be re-instantiated (like in this example), but if you change other things, like functions, the state is preserved and the new function will be what is called the next time that function is needed. All in all, this is very HOT! (You do need to have the nativescript-dev-webpack as a developer dependency of your project for HMR support)

NativeScript Preview

QR Code

Preview allows you to preview the application without having xcode or android tools installed and building it. It uses the already built playground preview application to preview the app so that you can see the app on your mobile phone. It has the same limitations as the playground site; but for most developers getting started it is a quick and easy way to start playing around with programming application. When you type:tns preview

you will then see the qr code in your console. Point the playground app's qr scanner at it and then your app will start running on your phone. Please note the entire application is uploaded to the cloud; which then your phone links to. So if this is an issue with your company don't use this feature.

Internal Changes

Android Core modules is now descended from the Google Support libraries; this opens up many more visual plugins and makes the app a lot more modern and compatible. One word of caution this did require them to do a bit of reworking of several parts of the base code for android. Their is the likely potential for new bugs to appears in areas that were bug free before. So heavily test your app before releasing it to the stores.

iOS Core modules now have a new way to do the safe areas; this also has required a lot of reworking and so the same warning applies. Test your app thoroughly to make sure that something in this area won't cause you any issues.

Updating NativeScript

To get updated; you first need to do:npm i -g nativescript@latest

That will get you the latest version of NativeScript CLI; once you have it; do a "tns --version" and verify it prints out "5.x.x". Then do a "tns doctor" to verify your environment is up to date and has all the newest support tools you need for a successful build.

Once thing I was made aware of today was that not everyone knows the magic "debugger;" command. I suppose this makes sense; I rarely see it mentioned, but it is a very useful debugging command in the JavaScript world.

The "debugger;" command is used to cause the application to stop and enter the debugger at that point. This works in Node.js, the browsers, and even in NativeScript! In a browser the developer tools must be open; if your debugger tools are closed; it will ignore it. In node, a debugger must already be attached or it will also ignore it.

In NativeScript it will actually halt the program and WAIT until a debugger is attached! So don't release your code

You can then start the application on the device; and it will automatically stop right at that debugger; statement. Then at the command line type: tns run android --start and you will be in the debugger right at that line.

So here I was again minding my own business on the forums again, and Ben posted he had been attempting to use this new feature of v1.5.x of NativeScript and it was failing. I responded it didn't work like that. Thankfully he was persistent and ignored my wrong response, and linked to the github issue a second time where they discuss the new feature. Him, being persistent despite me telling him it doesn't work that way; is why we now have a way to make it work! So thank you Ben!

So I went and looked at the new feature and found that by default it doesn't work as a normal person would expect. It does exactly what I expected and why I said it doesn't work. However, I decided to see if I could make it work the way any sane person would want it to and guess what, I have a method to make this work perfectly.

To make this work properly; you need NativeScript v1.5.1 or later; so type tns --version and make sure you are running v1.5.1 and later, if not upgrade.

WARNING: changing things in your platforms folder can completely break your build; and if you totally break your build you can do a tns platform remove android and then a tns platform add android to reset it.

First, you will need to navigate to your /platforms/android/src/main and copy the AndroidManifest.xml file to your /app/App_Resources/Android/ folder. This is going to be your primary manifest and the one you will edit to your hearts desire. Make sure you COPY just your app manifest.

Next you will need to edit the /platforms/android/src/main/AndroidManifest.xml and basically deleted everything in it. This file should end up looking like this:

Do not copy the above as is; you need to make sure the "package" property contains your app package name, which is why I recommend you edit the one in their as it already has your app package in it. If you copy the example above, it has YOUR_APP_PACKAGE which is an invalid package name.

Because the manifests are merged; and since the package name doesn't change the merging system is able to merge everything in your brand new App_Resources/Android/AndroidManifest.xml into the original AndroidManifest.xml file without any conflicts!

WooHoo! It has been such a long road to get this book published. I wrote a bit about it here. It is finally officially available on the Packt site for sale today! It should be available on the Amazon store by Monday the 1st of February. I will have a list of additional sites and urls later this week. If you want the best book on NativeScript (I know I'm so funny! I get to claim it because its the only book on NativeScript!) then it is now available!

I figured I would add a post for this; since I've done one on most the other version. v1.4 & v1.5 are the most seamless upgrades so far. Kudos to the Telerik teams involved in this. You can now upgrade in like four simple steps.

1. npm install -g nativescript
Ouch, that hurt, that was so dang difficult. Oh, wait no that must have been my last dentist visit. That was pretty darn seamless; and look if you now look at your nativescript --version you should have a nice v1.5.0 show up.

2. npm install tns-core-modules@latest --save
What, that is it to install all the new core modules; holy smokes Batman, it can't be that easy!

3. tns platform remove android AND/OR tns platform remove ios
Please note before you run the above commands if you have made ANY changes to the xcode project or the Android manifest; you might want to back them up first, or you will have to manually make those changes again.

To update you need to upgrade a couple items, recommended that you do it in order. Also, a word of caution once you upgrade your NativeScript CLI, you need to upgrade your platform and common modules for any of your other projects. It is supposed to be backwards compatible; but I was not successful in getting the new version of TNS CLI to compile an older project properly. Rather than spend any amount of time trying to get it to work, it was simpler just to upgrade the platform and common core modules...

Make sure you are NOT running Node v4.00; The current v4.00 version of Node does NOT work. If you need to downgrade node, I recommend you grab the latest version of Node v0.12.x from https://nodejs.org/download/release/latest-v0.12.x/. If you are already running node and it is working for you; you don't need to upgrade it. This note is just to make sure you DON'T upgrade node to v4.00.

Next thing if you are doing anything with Android; you want to do is type "gradle --version" and see if it runs. If it doesn't run from the command line you need to either install gradle or set your path to use it. If you have Android Studio installed; gradle is including with Android Studio, so you don't have to install it again. For example on my Windows machine; my gradle is located at: C:\Program Files (x86)\Android\android-studio\gradle\gradle-2.4\bin. So I added that to my path, so now gradle can run from anywhere. If you are using Ubuntu, the version included is really old and you will need to install a ppa from: https://launchpad.net/~cwchien/+archive/ubuntu/gradle Then you will be able to do a sudo apt-get update && sudo apt-get install gradle and get a much more recent version. On a Macintosh, it is recommended you install brew, and then do a brew install gradle. You can alternatively download and install it directly from https://gradle.org/. You must be running at least v2.3 of Gradle.

Edit 2015/09/17: The new version of the build system uses gradle and the gradle configuration file REQURIES that you are running v22.0.1 of the Android Build tools. In the grunt based build system you could use v21 or v23.0.1. Since this has caught a few people; you will want to open up the Android SDK installer, and make sure to install Build Tools v22.0.1

The next thing you need to make sure is that you have your ANDROID_HOME and JAVA_HOME environmental variables set. If you don't have them set you will get error messages about these needing to be set. On Windows you can do echo %ANDROID_HOME% %JAVA_HOME% to see if they are set. On Macintosh and Linux you can do echo $ANDROID_HOME $JAVA_HOME to see them.

So, now that you have your environment all setup; we need to upgrade the NativeScript Command Line utility, first. This is very important, before you even attempt to upgrade the platform modules you MUST be running the new version of the NativeScript CLI.

The easiest way is to do a: npm remove nativescript -g Yes, we want to de-install the current version; trust me it is easier this way. You can do a in-place update; but the times I tried it it threw errors; and I had to do it a couple times to get it installed. So, it is much easier just to de-install it. Then you just type: npm install nativescript -g to re-install it.

If everything worked fine; you should be able to do: tns --version and you will see v1.3.0

The next piece you need to do is upgrade your platform. Before you upgrade; you will want to make sure you backup any changes you made in the platform folder as both methods can overwrite the project files. You can do it one of two ways; my way or the simpler way. Telerik recommends the simpler way; you do a tns platform update android or tns platform update ios. In most cases this works great. However, in some of the past updates it has left you with a broken platform and then you still had to do it my way to fix it. So rather than have the chance that you will get a broken platform, I of course will recommend my way.

My way is the nuclear option! tns platform remove android and then tns platform add android. I remove and then re-add back the platform. Now, this will totally DELETE your platform/android or platform/ios folders. So if you made any customization to your project files; I'm going to re-stress this -- you will want to back them up first.

The final piece is updating the common core tns-core-modules; in past versions of nativescript this was a bit of a pain to upgrade. No more, it is now very simple! The first thing you can do is just totally delete your app/tns_modules folder. It is not used any more, totally ignored and now a waste of disk space. Then you just type npm install tns-core-modules --save from inside your root project folder. This will install the core modules in the node_modules folder like any other nativescript plugin. So now in the future you can do a simple npm update tns-core-modules and have the latest version.

And finally after everything is all done; you can do a:tns prepare android
and/ortns prepare ios

And you should see it confirm everything is good to go... Happy hacking on your NativeScript application.

In a lot of larger web sites it is pretty common that you use several third party resources like JavaScript. However, this is a potential malicious door into your customers computer via your website. What happens if the third party resource is changed by someone who does not have your best interests at heart. Your page will still happily load the malware right onto your customers browsers. So what can you do about this?

Well I'm glad you asked. In the just released Chrome 45 (and soon in an upcoming Firefox release), they have added a awesome new feature to protect your customers (and your reputation). When you link to any resources in your web page; you can now use the integrity attribute to tell the browser that this file must match this hash to load and use this file.

The browser integrity attribute must support the sha 256, 384 and 512 hashes according to the w3 spec. For browsers that don't support this yet; then this won't do anything and the resources will load fine just like normal. But in browsers that do support this; when the browser downloads the resource it will hash it and verify the hash matches before allowing it to be used.

Or if you don't have openssl installed; you can also easily cheat by using Chrome. Just add the integrity with a bogus value; then reload the page. Chrome in the developer log will show you the computed hash for the file when it blocks it.