• This repository has been archived on 11/Mar/2020
  • Stars
    star
    234
  • Rank 171,630 (Top 4 %)
  • Language
    Haskell
  • License
    MIT License
  • Created over 9 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple JavaScript Profiler

sjsp - Simple JavaScript Profiler

Why?

This is a JavaScript profiler, injecting profiling codes into your JavaScript files.

Applications written in JavaScript are getting larger these days and more complicated. There are many JavaScript Frameworks and they sometimes make us difficult to profile our applications. The default profilers of the Web Browsers get useless as our applications become huge and the frameworks do tricky things. The profiler sometimes lists many many functions of the frameworks.

Let's get back to what we really need.

We want to profile the code we write.

In a really simple way.

function test() {
  var start_time = Date.now(); // grab the current time at the top

  // our code

  log_profile("test", Date.now() - start_time); // grab the current time again and log the time the function consumed.
}

Do we have to write the profiling codes in all the functions by hand?

Of course not.

So, here comes sjsp, a tool for injecting profiling codes into JavaScript files.

Installation

Homebrew

brew install itchyny/tap/sjsp

Build with stack

git clone https://github.com/itchyny/sjsp
cd sjsp
stack install
export PATH=$PATH:$HOME/.local/bin

Usage

 $ sjsp test.js            # generates test.sjsp.js
  1. Use sjsp command on the JavaScript files you want to profile. The command sjsp does not break your test.js, but creates a new file.
  2. Use the generated test.sjsp.js instead of test.js
  3. Open the page with your favorite browser and look into the JavaScript console. The profiling result will be reported every 10 seconds.

The profiling result will look like the following.

========== SORT BY TIME ==========
time: 30.202sec   count:  71      test6  test.js  (line: 31, col: 18)  function test6() {
time: 16.474sec   count:  41      test7  test.js  (line: 37, col: 18)  function test7() {
time: 15.490sec   count: 133      test4  test.js  (line: 19, col: 18)  function test4() {
time:  5.981sec   count: 216      test1  test.js  (line:  1, col: 18)  function test1() {
time:  4.375sec   count:  18      test5  test.js  (line: 25, col: 18)  function test5() {
time:  3.241sec   count: 512      test3  test.js  (line: 13, col: 18)  function test3() {
time:  0.874sec   count:  67  anonymous  test.js  (line: 49, col: 24)  setInterval(function() {
time:  0.808sec   count:   2      test2  test.js  (line:  7, col: 18)  function test2() {
time:  0.445sec   count:   2  anonymous  test.js  (line: 43, col: 23)  setTimeout(function() {
========== SORT BY COUNT ==========
time:  3.241sec   count: 512      test3  test.js  (line: 13, col: 18)  function test3() {
time:  5.981sec   count: 216      test1  test.js  (line:  1, col: 18)  function test1() {
time: 15.490sec   count: 133      test4  test.js  (line: 19, col: 18)  function test4() {
time: 30.202sec   count:  71      test6  test.js  (line: 31, col: 18)  function test6() {
time:  0.874sec   count:  67  anonymous  test.js  (line: 49, col: 24)  setInterval(function() {
time: 16.474sec   count:  41      test7  test.js  (line: 37, col: 18)  function test7() {
time:  4.375sec   count:  18      test5  test.js  (line: 25, col: 18)  function test5() {
time:  0.808sec   count:   2      test2  test.js  (line:  7, col: 18)  function test2() {
time:  0.445sec   count:   2  anonymous  test.js  (line: 43, col: 23)  setTimeout(function() {

The result is easy to read and shows the functions you have to improve the performance of.

How it works

Suppose test.js looks like the following.

function test() {
  console.log('test');
}

The sjsp command generates test.sjsp.js.

/* some dirty codes of sjsp */ function test() { var sjsp__state = sjsp__start("test.js",1,1,"test","function test() {");
  console.log('test');; sjsp__end(sjsp__state);
}

It simply inserts sjsp__start and sjsp__end function calls at the top and the end of the functions. The local variable sjsp__state holds the starting time. It also saves the name, line number and column number of the function and the whole line. When the sjsp__end function is called, the profiling result is stored.

It just inserts the two statements for each functions. However, remember that functions can be aborted with return statements. How does it handle return statements?

Suppose the expression which is returned by the function is heavy.

function test() {  
  return someHeavyExpression;
}

Firstly consider the following code.

function test() { var sjsp__state = sjsp__start("test.js",1,1,"test","function test() {  ");  
  return someHeavyExpression; sjsp__end(sjsp__state);
}

Unfortunately, the sjsp__end function will never be called. Then what about placing the function before the return statement?

function test() { var sjsp__state = sjsp__start("test.js",1,1,"test","function test() {  ");  
  sjsp__end(sjsp__state); return someHeavyExpression;
}

The function will surely be called but the profiling result is not correct. Now, let's see how sjsp handles return statements.

function test() { var sjsp__state = sjsp__start("test.js",1,1,"test","function test() {  ");  
  return (function(arguments){ var sjsp__return = someHeavyExpression; sjsp__end(sjsp__state); return sjsp__return; } ).call(this,arguments);; sjsp__end(sjsp__state);
}

It creates an anonymous function, captures the result and calls the function instantly. This way does not break the logic and the profiling result is correct.

Author

itchyny (https://github.com/itchyny)

License

This software is released under the MIT License, see LICENSE.

More Repositories

1

lightline.vim

A light and configurable statusline/tabline plugin for Vim
Vim Script
6,540
star
2

gojq

Pure Go implementation of jq
Go
3,016
star
3

calendar.vim

A calendar application for Vim
Vim Script
1,904
star
4

bed

Binary editor written in Go
Go
1,134
star
5

mmv

rename multiple files with editor
Go
735
star
6

vim-cursorword

Underlines the word under the cursor
Vim Script
581
star
7

vim-gitbranch

Provides the branch name of the current git repository
Vim Script
199
star
8

timefmt-go

Efficient time formatting library (strftime, strptime) for Golang
Go
177
star
9

rexdep

Roughly extract dependency relation from source code
Go
171
star
10

fillin

fill-in your command and execute
Go
142
star
11

landscape.vim

A colorscheme for Vim
Vim Script
137
star
12

maze

A maze command written in Go
Go
115
star
13

base58-go

Base58 encoding/decoding package and command written in Go
Go
106
star
14

vim-haskell-indent

If the plugin does not work for some syntax, feel free to report to the issue tracker!
Vim Script
105
star
15

thumbnail.vim

A thumbnail-style buffer selector for Vim
Vim Script
101
star
16

volume-go

Cross-platform audio volume control library for Go
Go
72
star
17

rassemble-go

Go implementation of Regexp::Assemble
Go
69
star
18

vim-qfedit

Edit the quickfix/location list freely
Vim Script
65
star
19

miv

Vim plugin manager written in Haskell
Haskell
65
star
20

json2yaml

An efficient JSON to YAML converter written in Go language
Go
64
star
21

vim-parenmatch

An efficient alternative to the standard matchparen plugin
Vim Script
59
star
22

dictionary.vim

Dictionary.app interface for Vim
Vim Script
58
star
23

screensaver.vim

Screensavers for Vim
Vim Script
53
star
24

gojo

Yet another Go implementation of jo
Go
49
star
25

fastinvsqrt

Fast inverse square root in programming languages
Makefile
49
star
26

github-migrator

GitHub repository migrator
Go
46
star
27

vim-highlighturl

URL highlight everywhere
Vim Script
45
star
28

dotfiles

my dotfiles
Vim Script
37
star
29

llvm-brainfuck

Brainfuck compiler based on LLVM API
C++
35
star
30

mkrg

Mackerel graph viewer in terminal
Go
34
star
31

qhs

SQL queries on CSV and TSV files
Haskell
33
star
32

lightline-powerful

Powerful settings for lightline.vim
Vim Script
26
star
33

procout

procout peeks write(2) of another process using ptrace(2), written in Rust
Rust
26
star
34

s3-cache-action

GitHub Action to save cache files and restore them from Amazon S3
TypeScript
22
star
35

event-go

Simple synchronous event pub-sub package for Golang
Go
20
star
36

github-better-header

Brings back a better GitHub header
HTML
20
star
37

cam

unix command cam: view images inside terminal
C
20
star
38

vim-winfix

Fix the focus and the size of windows in Vim
Vim Script
18
star
39

setup

DO NOT USE THIS
Shell
15
star
40

pihex-rs

Arbitrary place hexadecimal digits viewer of pi written in Rust
Rust
15
star
41

vim-gof

Vim Script
13
star
42

minivm

C
13
star
43

vim-external

Switch to external applications from Vim
Vim Script
11
star
44

mackerel-plugin-rs

Mackerel plugin helper library for Rust
Rust
11
star
45

jsparser

A JavaScript parser in JavaScript generated by Jison
CoffeeScript
11
star
46

ChromePlayer

A music player for local files, working on Google Chrome
JavaScript
11
star
47

astgen-go

interface{} => ast.Node
Go
9
star
48

screensaver.c

A clock screensaver in terminal
C
9
star
49

uptime-rs

Multi-platform uptime library for Rust
Rust
9
star
50

golang-simple-server-sample

A simple server sample in Go
Go
8
star
51

vim-pdf

pdf filetype plugin for Vim
Vim Script
8
star
52

shell-function-and

shell function: and
8
star
53

vim-grep

The only grep in Vim that I need...
Vim Script
8
star
54

zshhist-go

zsh histfile utility for Go
Go
7
star
55

brainfuck

brainfuck
Brainfuck
7
star
56

bin

My utility executables.
Shell
7
star
57

git-branch-name

Optimally fast branch name command for Git.
C
7
star
58

homebrew-tap

Homebrew formulae
Ruby
6
star
59

mackerel-client-rs

An API client library for Mackerel written in Rust (still in the developing stage; host APIs are not implemented yet)
Rust
6
star
60

minivm-go

Golang implementation of a stack-machine based programming language interpreter
Go
6
star
61

maze-c

unix command maze: generating a maze
C
6
star
62

vim-haskell-sort-import

Sort import statements in Haskell codes
Haskell
6
star
63

mackerel-plugin-battery

Battery plugin for Mackerel
Go
5
star
64

vim-closebuffer

Close buffers in Vim
Vim Script
5
star
65

vim-extracmd

Define extra commands.
Vim Script
5
star
66

unite-preview

A preview plugin for vimfiler, unite
Vim Script
4
star
67

mackerel-client-hs

Mackerel API client in Haskell
Haskell
4
star
68

2bf

2bf - generates a Brainfuck code
C
4
star
69

vim-term

Vim Script
3
star
70

maketen-go

Create 10 from numbers!
Go
3
star
71

vim-cmdline-ranges

Quickly start/edit cmdline-ranges in Vim
Vim Script
3
star
72

Filter.js

A sample of image processing with JavaScript and canvas
JavaScript
3
star
73

vim-quickrun-lightline-hooks

Vim Script
3
star
74

vim-autoft

Set filetype automatically in Vim
Vim Script
3
star
75

itchyny

itchyny's profile page
3
star
76

tie

Go
3
star
77

fractal

fractal figures in gnuplot
3
star
78

procalive

procalive keeps your process alive
Rust
3
star
79

zsh-auto-fillin

Automatic fillin https://github.com/itchyny/fillin
Shell
3
star
80

c2bf.hs

Convert C to Brainfuck (not working, deprecated product)
Haskell
2
star
81

homebrew-rexdep

Deprecated in favor of https://github.com/itchyny/homebrew-tap
2
star
82

vim-cmdline-escape

Escape special characters on cmdline
Vim Script
2
star
83

vim-extra-snippets

My own snippets
Vim Snippet
2
star
84

bf

bf - executes a Brainfuck code
C
2
star
85

setupfiles-go

Create files and directories easily for tests in Go
Go
2
star
86

syscall-study

Go
2
star
87

vim-histexclude

Exclude by patterns from the histories.
Vim Script
2
star
88

vim-spellbad-pattern

Register regexps to SpellBad
Vim Script
2
star
89

mackerel-plugin-dice-sh

Dice plugin for Mackerel
Shell
2
star
90

unite-auto-open

starting or opening files action for unite
Vim Script
2
star
91

mtimedir

Go
2
star
92

sbt-compile-warn

Aggregate sbt compile warnings
Go
2
star
93

codeforces

Codeforces in Haskell
Haskell
2
star
94

browsershell

A shell in your browser
JavaScript
2
star
95

autolatex

A shell script to compile a LaTeX file
Shell
2
star
96

formulate

Homebrew formula managing script
Shell
2
star
97

mmpp

Mackerel metric pretty printer
Rust
1
star
98

atcoder

AtCoder in Rust
Rust
1
star
99

mackerel-plugin-dice-rs

Dice plugin example using mackerel-plugin-rs
Rust
1
star
100

unite-changetime

An action for unite/vimfiler, changing modified time of a file
Vim Script
1
star