1.4. Running Analysis Locally

Let’s test our analyzer! First we’ll need a target JavaScript project to test it on. Let’s use the popular left-pad project. Make a folder to keep our test projects in and clone left-pad to the folder:

$ mkdir ~/test-repos
$ git clone https://github.com/stevemao/left-pad ~/test-repos/left-pad

Now we can run our analyzer:

$ r2c run ~/test-repos/left-pad

We get the following output:

$ r2c run ~/test-repos/left-pad/
🏃 Running analyzer...
Counting whitespace in file ./test.js
Counting whitespace in file ./perf/es6Repeat.js
Counting whitespace in file ./perf/perf.js
Counting whitespace in file ./perf/O(n).js
Counting whitespace in file ./index.js
{
    "results": [
...

Notice how the print statements in our Python code appear as if we were running the program outside of r2c. This makes it easy to debug programs normally. The full JSON output of the program should look something like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
{
    "results": [
        {
            "check_id": "whitespace",
            "extra": {
                "num_whitespace": 638,
                "size": 3673
            },
            "path": "./test.js"
        },
        {
            "check_id": "whitespace",
            "extra": {
                "num_whitespace": 61,
                "size": 216
            },
            "path": "./perf/es6Repeat.js"
        },
        {
            "check_id": "whitespace",
            "extra": {
                "num_whitespace": 213,
                "size": 1442
            },
            "path": "./perf/perf.js"
        },
        {
            "check_id": "whitespace",
            "extra": {
                "num_whitespace": 77,
                "size": 241
            },
            "path": "./perf/O(n).js"
        },
        {
            "check_id": "whitespace",
            "extra": {
                "num_whitespace": 360,
                "size": 1137
            },
            "path": "./index.js"
        }
    ]
}

Cool! Now we can hunt for minified files. All of these files look reasonable; though we don’t know exactly what our threshold should be, minified files will probably be less than 5 to 10 percent whitespace. To find minified files in top projects and get a sense of what cut-off point to use, we’ll want to run this analyzer at scale against npm packages.

To get started, head on over to Running on r2c.