Skip to content

How to profile JavaScript

Alexander Vakrilov edited this page Apr 28, 2017 · 9 revisions

Note: this functionality may change in future

NativeScript runtime for Android provides __startCPUProfiler and __stopCPUProfiler global functions which help you to profile your JavaScript code. To enable these methods you need to set the profilerOutputDir key in your app/package.json next to v8Flags inside the android section:

{
  "android": {
    "v8Flags": "--expose_gc",
    "profilerOutputDir": "/sdcard/"
  },
  "main": "main.js",
  ...
}

Here is an example usage:

__startCPUProfiler("myprofile");
// some JavaScript code
__stopCPUProfiler("myprofile");

This will create a new file with the following naming scheme /sdcard/<package name>-<profile>-<timestamp>.cpuprofile. For example, if your application package name is org.nativescript.app1 then the output file will be named something like org.nativescript.app1-myprofile-19388793.672149.cpuprofile. Once you have a *.cpuprofile file you can pull it from the device with adb pull command. In our case the command will be:

adb pull /sdcard/org.nativescript.app1-myprofile-19388793.672149.cpuprofile

NOTE: If there is not .cpuprofile file generated - try giving the app the storage permission explicitly by going to settings -> app -> <myapp> -> permission and enable the storage permission.

Then you can load the file with Chrome DevTools.