-
Notifications
You must be signed in to change notification settings - Fork 3
/
behavior.js
157 lines (146 loc) · 4.82 KB
/
behavior.js
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Polyfill performance.now
performance.now = function () {return performance.now || performance.mozNow || performance.msNow || performance.oNow || performance.webkitNow || function () {return new Date().getTime()}}()
var results = []
var workers = []
var round = -1
var chart
var userInputs
var mode
function setMode () {
mode = $('.inputArea select').val()
$('#script').attr({'placeholder': sampleFunctions[mode].toString()})
userInputs = JSON.parse($('.results textarea').val())
if (_.find(sampleInputs, function (input) {return "" + input === "" + userInputs})) {
userInputs = sampleInputs[mode]
$('.results textarea').val(JSON.stringify(userInputs))
}
}
$(function () {
$('.results textarea').val(JSON.stringify(sampleInputs['number']))
setMode()
})
function getInput () {
userInputs = JSON.parse($('.results textarea').val())
var func = $('#script').val()
if (!func) {
func = sampleFunctions[mode]
$('#script').val(func.toString())
}
// Convert function expressions to function declarations
func = func.toString().replace(/^var\s\w+\s?=\s?/, '')
// Remove trailing semicolon, which screws up eval
func = func.toString().replace(/;\s*$/, '')
$('#script').val(func)
return func
}
function run () {
$('.results p').hide()
// $('.results ul').text('')
round++
results.push([])
workers.push([])
var func = getInput();
(function (round) {
userInputs.forEach(function (input) {
if (!!window.Worker) {
// setTimeout(function () {
workers[round].push(new Worker("worker.js"))
var message = []
message.push(JSON.stringify(func))
message.push(JSON.stringify(input))
workers[round][workers[round].length - 1].postMessage(message)
workers[round][workers[round].length - 1].onmessage = function(e) {
if (!chart) {
generateChart()
}
print(input, e.data, round)
}
// }, 50)
} else {
setTimeout(print.bind(null, input, profile(func, input), round), 0)
}
})
})(round)
}
function print (size, time, round) {
var inputSizes = userInputs.map(function (input) {
if (mode !== 'number') {
return input.length
}
return input
})
// $('.results ul').append('<li><span class="size">'+size+'</span>: <span class="time">'+time+'</span>ms</li>')
results[round].push(time)
var xes = {}
xes['round ' + (round + 1)] = 'size ' + (round + 1)
chart.load({
columns: [
[function(){return 'size ' + (round + 1)}()].concat(inputSizes),
[function(){return 'round ' + (round + 1)}()].concat(results[round])
],
xs: xes,
})
}
function generateChart () {
var format = d3.format(',')
chart = c3.generate({
bindto: '#chart',
legend: {show: false},
data: {columns: []},
axis: {
y: {label: {text: 'milliseconds (ms)', position: 'outer-middle'}},
x: {label: {text: 'input size (n)', position: 'outer-center'}}
},
tooltip: {
format: {
title: function (d) { return 'n = ' + format(d) },
value: function (value) {return format(value) + " ms"}
}
}
})
}
var QueryString = function () {
// This function is anonymous, is executed immediately and
// the return value is assigned to QueryString!
var query_string = {};
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
// If first entry with this name
if (typeof query_string[pair[0]] === "undefined") {
query_string[pair[0]] = pair[1];
// If second entry with this name
} else if (typeof query_string[pair[0]] === "string") {
var arr = [ query_string[pair[0]], pair[1] ];
query_string[pair[0]] = arr;
// If third or later entry with this name
} else {
query_string[pair[0]].push(pair[1]);
}
}
return query_string;
} ();
function checkURLparams() {
if (QueryString.inputType) {
// should already be a string
// check if its an acceptable type ['number', 'array', 'string']
// if so, overwrite the input dropdown to match that type
}
if (QueryString.inputFunction) {
// probably a string? with new lines? and spaces? does it need to be JSON parsed?
// lets stick it in the input function textarea.
}
if (QueryString.inputArgs) {
// hopefully a string
// stick it in the input arguments textarea
}
}
function generateURLparams () {
// like checkURLparams, but in reverse!
// save the inputType, inputFunction, and inputArgs as strings
// construct a url parameter for these three values
// stick it onto the current url, with a `?`
// we might need to clear the current window.location.search
// and then set window.location? so it can just be copied and pasted? simplest solution for now
}