Skip to content

Instantly share code, notes, and snippets.

@thomas4g
Created May 5, 2018 20:55
Show Gist options
  • Save thomas4g/1c987331869286f8151a4426a32e1f4b to your computer and use it in GitHub Desktop.
Save thomas4g/1c987331869286f8151a4426a32e1f4b to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/jigawid
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
let Pipeline = {
data: [],
ops: [],
get: function () {
let res = []
for (let i = 0; i < this.data.length; i++) {
let val = this.data[i]
let passesFilters = true
for (let j = 0; passesFilters && j < this.ops.length; j++) {
let op = this.ops[j]
switch (op.type) {
case 'map':
val = op.func(val)
break;
case 'filter':
passesFilters = op.func(val)
break;
default:
throw new Exception('Illegal op type')
}
}
if (passesFilters) {
res.push(val)
}
}
return res
},
make_new: function (data) {
return Object.create(Pipeline, { data: { value: data }})
},
add_op: function (type, func) {
this.ops.push({
type: type,
func: func
})
},
map: function (func) {
this.add_op('map', func)
return this
},
filter: function (func) {
this.add_op('filter', func)
return this
}
}
let sample_data = []
for (let i = 0; i < 1000000; i++) {
sample_data[i] = Math.random() * 100;
}
let t0 = performance.now()
Pipeline.make_new(sample_data)
.map(el => el * 2)
.map(el => el + 15)
.map(el => el / 3)
.map(el => el + 15)
.filter(el => el > 50)
.get()
let t1 = performance.now()
console.log('Custom pipeline took ' + (t1 - t0) + 'ms')
t0 = performance.now()
sample_data
.map(el => el * 2)
.map(el => el + 15)
.map(el => el / 3)
.map(el => el + 15)
.filter(el => el > 50)
t1 = performance.now()
console.log('built-in map/filter chaining took ' + (t1 - t0) + 'ms')
t0 = performance.now()
let res = []
for (let i = 0; i < sample_data.length; i++) {
let val = (sample_data[i] * 2 + 15) / 3 + 15
if (val > 50) {
res.push(val)
}
}
t1 = performance.now()
console.log('OG for loop took ' + (t1 - t0) + 'ms')
</script>
<script id="jsbin-source-javascript" type="text/javascript">let Pipeline = {
data: [],
ops: [],
get: function () {
let res = []
for (let i = 0; i < this.data.length; i++) {
let val = this.data[i]
let passesFilters = true
for (let j = 0; passesFilters && j < this.ops.length; j++) {
let op = this.ops[j]
switch (op.type) {
case 'map':
val = op.func(val)
break;
case 'filter':
passesFilters = op.func(val)
break;
default:
throw new Exception('Illegal op type')
}
}
if (passesFilters) {
res.push(val)
}
}
return res
},
make_new: function (data) {
return Object.create(Pipeline, { data: { value: data }})
},
add_op: function (type, func) {
this.ops.push({
type: type,
func: func
})
},
map: function (func) {
this.add_op('map', func)
return this
},
filter: function (func) {
this.add_op('filter', func)
return this
}
}
let sample_data = []
for (let i = 0; i < 1000000; i++) {
sample_data[i] = Math.random() * 100;
}
let t0 = performance.now()
Pipeline.make_new(sample_data)
.map(el => el * 2)
.map(el => el + 15)
.map(el => el / 3)
.map(el => el + 15)
.filter(el => el > 50)
.get()
let t1 = performance.now()
console.log('Custom pipeline took ' + (t1 - t0) + 'ms')
t0 = performance.now()
sample_data
.map(el => el * 2)
.map(el => el + 15)
.map(el => el / 3)
.map(el => el + 15)
.filter(el => el > 50)
t1 = performance.now()
console.log('built-in map/filter chaining took ' + (t1 - t0) + 'ms')
t0 = performance.now()
let res = []
for (let i = 0; i < sample_data.length; i++) {
let val = (sample_data[i] * 2 + 15) / 3 + 15
if (val > 50) {
res.push(val)
}
}
t1 = performance.now()
console.log('OG for loop took ' + (t1 - t0) + 'ms')
</script></body>
</html>
let Pipeline = {
data: [],
ops: [],
get: function () {
let res = []
for (let i = 0; i < this.data.length; i++) {
let val = this.data[i]
let passesFilters = true
for (let j = 0; passesFilters && j < this.ops.length; j++) {
let op = this.ops[j]
switch (op.type) {
case 'map':
val = op.func(val)
break;
case 'filter':
passesFilters = op.func(val)
break;
default:
throw new Exception('Illegal op type')
}
}
if (passesFilters) {
res.push(val)
}
}
return res
},
make_new: function (data) {
return Object.create(Pipeline, { data: { value: data }})
},
add_op: function (type, func) {
this.ops.push({
type: type,
func: func
})
},
map: function (func) {
this.add_op('map', func)
return this
},
filter: function (func) {
this.add_op('filter', func)
return this
}
}
let sample_data = []
for (let i = 0; i < 1000000; i++) {
sample_data[i] = Math.random() * 100;
}
let t0 = performance.now()
Pipeline.make_new(sample_data)
.map(el => el * 2)
.map(el => el + 15)
.map(el => el / 3)
.map(el => el + 15)
.filter(el => el > 50)
.get()
let t1 = performance.now()
console.log('Custom pipeline took ' + (t1 - t0) + 'ms')
t0 = performance.now()
sample_data
.map(el => el * 2)
.map(el => el + 15)
.map(el => el / 3)
.map(el => el + 15)
.filter(el => el > 50)
t1 = performance.now()
console.log('built-in map/filter chaining took ' + (t1 - t0) + 'ms')
t0 = performance.now()
let res = []
for (let i = 0; i < sample_data.length; i++) {
let val = (sample_data[i] * 2 + 15) / 3 + 15
if (val > 50) {
res.push(val)
}
}
t1 = performance.now()
console.log('OG for loop took ' + (t1 - t0) + 'ms')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment