Marshalling Arrays in NativeScript for iOS - The Road to Perfection

We at NativeScript take performance very seriously, so we try to improve it in each release. In this blog post I want to show you how marshalling between JavaScript arrays and Objective-C arrays has evolved between four different versions of the NativeScript runtime for iOS.

If you are not familiar with the idea of marshalling in general, please take a look at the docs: Marshalling overview.

v0.1 - Creating a Deep Copy in JavaScript

In the very first release of NativeScript for iOS, if some method required NSArray as an argument, the user had to manually create a NSArray copy of the JavaScript array before calling the method:

function JSArrayToNSArray(jsArray) {
let nsArray = new NSMutableArray();
for (let currentJSObject of jsArray) {
// Objective-C Arrays can't contain nil, so use NSNull object instead.
// Marshalling of each element of the array is still done in native.
nsArray.addObject(currentJSObject != null ? currentJSObject : NSNull.null());
return nsArray;

var textChecker = new UITextChecker(); // Shorthand for alloc/init

This was the easiest way for us to implement it and in fact required no special native code. Needless to say, the code didn’t look as nice as it could and the performance of the testing apps suffered, but it was a great start to get something working.

Under the Curtains of <HTMLView /> for NativeScript for iOS

We just released version 1.2 of NativeScript which among many new features included a new <HTMLView /> element. I’ll show you what stands behind it with nothing but plain JavaScript running on the iOS runtime.

We are going to start with the iOS “Hello, World!” template and we will replace the contents of the label from NSString with NSAttributedString. On iOS the <HTMLView /> element uses NSAttributedString to render itself. You can read more about it in the Apple docs, but in short “it manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string”.