chapters: delete old chapter samples
This commit is contained in:
@@ -1,23 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const MongoClient = require('mongodb').MongoClient
|
||||
const url = 'mongodb://localhost:27017/test';
|
||||
var count = 0
|
||||
var max = 1000
|
||||
|
||||
MongoClient.connect(url, function(err, db) {
|
||||
if (err) { throw err }
|
||||
const collection = db.collection('data')
|
||||
const average = db.collection('averages')
|
||||
|
||||
collection.find({}).toArray(function (err, data) {
|
||||
if (err) { throw err }
|
||||
average.insert({
|
||||
value: data.reduce((acc, v) => acc + v, 0) / data.length
|
||||
}, function (err) {
|
||||
if (err) { throw err }
|
||||
db.close()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const MongoClient = require('mongodb').MongoClient
|
||||
const url = 'mongodb://localhost:27017/test';
|
||||
var count = 0
|
||||
var max = 1000
|
||||
|
||||
MongoClient.connect(url, function(err, db) {
|
||||
if (err) { throw err }
|
||||
const collection = db.collection('data')
|
||||
|
||||
function insert (err) {
|
||||
if (err) throw err
|
||||
|
||||
if (count++ === max) {
|
||||
return db.close()
|
||||
}
|
||||
|
||||
collection.insert({
|
||||
value: Math.random() * 1000000
|
||||
}, insert)
|
||||
}
|
||||
|
||||
insert()
|
||||
})
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
{
|
||||
"name": "async-opt",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"express": "^4.14.0",
|
||||
"fastq": "^1.4.1",
|
||||
"lru-cache": "^4.0.1",
|
||||
"mongodb": "^2.1.18"
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const MongoClient = require('mongodb').MongoClient
|
||||
const express = require('express')
|
||||
const app = express()
|
||||
|
||||
var url = 'mongodb://localhost:27017/test';
|
||||
|
||||
MongoClient.connect(url, function(err, db) {
|
||||
if (err) { throw err }
|
||||
const collection = db.collection('data')
|
||||
app.get('/hello', (req, res) => {
|
||||
collection.findOne({}, function sum (err, data) {
|
||||
res.send('' + data.value)
|
||||
})
|
||||
})
|
||||
|
||||
app.listen(3000)
|
||||
})
|
||||
@@ -1,56 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const MongoClient = require('mongodb').MongoClient
|
||||
const express = require('express')
|
||||
const LRU = require('lru-cache')
|
||||
const fastq = require('fastq')
|
||||
const app = express()
|
||||
|
||||
var url = 'mongodb://localhost:27017/test';
|
||||
|
||||
function sum (data) {
|
||||
var sum = 0
|
||||
const l = data.length
|
||||
for (var i = 0; i < l; i++) {
|
||||
sum += data[i].value
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
MongoClient.connect(url, function(err, db) {
|
||||
if (err) { throw err }
|
||||
const collection = db.collection('data')
|
||||
const queue = fastq(work)
|
||||
const cache = LRU({
|
||||
maxAge: 1000 * 5 // 5 seconds
|
||||
})
|
||||
|
||||
function work (req, done) {
|
||||
const elem = cache.get('average')
|
||||
if (elem) {
|
||||
done(null, elem)
|
||||
return
|
||||
}
|
||||
collection.find({}).toArray(function (err, data) {
|
||||
if (err) {
|
||||
done(err)
|
||||
return
|
||||
}
|
||||
const result = sum(data) / data.length
|
||||
cache.set('average', result)
|
||||
done(null, result)
|
||||
})
|
||||
}
|
||||
|
||||
app.get('/hello', (req, res) => {
|
||||
queue.push(req, function (err, result) {
|
||||
if (err) {
|
||||
res.send(err.message)
|
||||
return
|
||||
}
|
||||
res.send('' + result)
|
||||
})
|
||||
})
|
||||
|
||||
app.listen(3000)
|
||||
})
|
||||
@@ -1,29 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const MongoClient = require('mongodb').MongoClient
|
||||
const express = require('express')
|
||||
const app = express()
|
||||
|
||||
var url = 'mongodb://localhost:27017/test'
|
||||
|
||||
MongoClient.connect(url, function(err, db) {
|
||||
if (err) { throw err }
|
||||
const collection = db.collection('data')
|
||||
app.get('/hello', (req, res) => {
|
||||
collection.find({}).toArray(function sum (err, data) {
|
||||
if (err) {
|
||||
res.send(err)
|
||||
return
|
||||
}
|
||||
var sum = 0
|
||||
const l = data.length
|
||||
for (var i = 0; i < l; i++) {
|
||||
sum += data[i].value
|
||||
}
|
||||
const result = sum / data.length
|
||||
res.send('' + result)
|
||||
})
|
||||
})
|
||||
|
||||
app.listen(3000)
|
||||
})
|
||||
@@ -1,33 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const MongoClient = require('mongodb').MongoClient
|
||||
const express = require('express')
|
||||
const app = express()
|
||||
|
||||
var url = 'mongodb://localhost:27017/test';
|
||||
|
||||
function sum (data) {
|
||||
var sum = 0
|
||||
const l = data.length
|
||||
for (var i = 0; i < l; i++) {
|
||||
sum += data[i].value
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
MongoClient.connect(url, function(err, db) {
|
||||
if (err) { throw err }
|
||||
const collection = db.collection('data')
|
||||
app.get('/hello', (req, res) => {
|
||||
collection.find({}).toArray(function (err, data) {
|
||||
if (err) {
|
||||
res.send(err)
|
||||
return
|
||||
}
|
||||
const result = sum(data) / data.length
|
||||
res.send('' + result)
|
||||
})
|
||||
})
|
||||
|
||||
app.listen(3000)
|
||||
})
|
||||
@@ -1,26 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const MongoClient = require('mongodb').MongoClient
|
||||
const express = require('express')
|
||||
const app = express()
|
||||
|
||||
var url = 'mongodb://localhost:27017/test';
|
||||
|
||||
|
||||
MongoClient.connect(url, function(err, db) {
|
||||
if (err) { throw err }
|
||||
const collection = db.collection('data')
|
||||
app.get('/hello', (req, res) => {
|
||||
collection.find({}).toArray(function sum (err, data) {
|
||||
if (err) {
|
||||
res.send(err)
|
||||
return
|
||||
}
|
||||
const total = data.reduce((acc, d) => acc + d.value, 0)
|
||||
const result = total / data.length
|
||||
res.send('' + result)
|
||||
})
|
||||
})
|
||||
|
||||
app.listen(3000)
|
||||
})
|
||||
@@ -1,27 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const MongoClient = require('mongodb').MongoClient
|
||||
const express = require('express')
|
||||
const app = express()
|
||||
|
||||
var url = 'mongodb://localhost:27017/test';
|
||||
|
||||
|
||||
MongoClient.connect(url, function(err, db) {
|
||||
if (err) { throw err }
|
||||
const collection = db.collection('data')
|
||||
app.get('/hello', (req, res) => {
|
||||
var count = 0
|
||||
var result = 0
|
||||
collection.find({})
|
||||
.on('data', function (chunk) {
|
||||
count++
|
||||
result += chunk.value
|
||||
})
|
||||
.on('end', function () {
|
||||
res.send('' + (result / count))
|
||||
})
|
||||
})
|
||||
|
||||
app.listen(3000)
|
||||
})
|
||||
@@ -1,15 +0,0 @@
|
||||
{
|
||||
"name": "bench-http",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"main": "server.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "UNLICENSED",
|
||||
"dependencies": {
|
||||
"body-parser": "^1.15.2",
|
||||
"express": "^4.13.4"
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const express = require('express')
|
||||
const bodyParser = require('body-parser')
|
||||
const app = express()
|
||||
|
||||
app.use(bodyParser.json())
|
||||
app.use(bodyParser.urlencoded({extended: false}))
|
||||
|
||||
app.post('/echo', (req, res) => {
|
||||
res.send(req.body)
|
||||
})
|
||||
|
||||
app.listen(3000)
|
||||
@@ -1,14 +0,0 @@
|
||||
{
|
||||
"name": "bench-http",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"main": "server.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "UNLICENSED",
|
||||
"dependencies": {
|
||||
"express": "^4.13.4"
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
{
|
||||
"name": "bench-http",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"main": "server.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "UNLICENSED",
|
||||
"dependencies": {
|
||||
"express": "^4.13.4",
|
||||
"jade": "^1.11.0"
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const express = require('express')
|
||||
const path = require('path')
|
||||
const app = express()
|
||||
|
||||
app.set('views', path.join(__dirname, 'views'));
|
||||
app.set('view engine', 'jade');
|
||||
|
||||
app.get('/hello', (req, res) => {
|
||||
res.render('hello', { title: 'Express' });
|
||||
})
|
||||
|
||||
app.listen(3000)
|
||||
@@ -1,7 +0,0 @@
|
||||
doctype html
|
||||
html
|
||||
head
|
||||
title= title
|
||||
link(rel='stylesheet', href='/stylesheets/style.css')
|
||||
body
|
||||
h1= title
|
||||
@@ -1,10 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const express = require('express')
|
||||
const app = express()
|
||||
|
||||
app.get('/hello', (req, res) => {
|
||||
res.send('hello world')
|
||||
})
|
||||
|
||||
app.listen(3000)
|
||||
@@ -1,16 +0,0 @@
|
||||
{
|
||||
"name": "hello-server",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "David Mark Clements",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"express": "^4.14.0",
|
||||
"jade": "^1.11.0"
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
const express = require('express')
|
||||
const path = require('path')
|
||||
const app = express()
|
||||
|
||||
app.set('views', path.join(__dirname, 'views'));
|
||||
app.set('view engine', 'jade');
|
||||
|
||||
app.get('/hello', (req, res) => {
|
||||
res.render('hello', { title: 'Express' });
|
||||
})
|
||||
|
||||
app.listen(3000)
|
||||
@@ -1,7 +0,0 @@
|
||||
doctype html
|
||||
html
|
||||
head
|
||||
title= title
|
||||
link(rel='stylesheet', href='/stylesheets/style.css')
|
||||
body
|
||||
h1= title
|
||||
@@ -1,16 +0,0 @@
|
||||
const http = require('http')
|
||||
const starwarsName = require('starwars-names').random
|
||||
const names = {}
|
||||
|
||||
http.createServer((req, res) => {
|
||||
res.end(`Your unique name is: ${createName(req)} \n`)
|
||||
}).listen(8080)
|
||||
|
||||
function createName () {
|
||||
var result = starwarsName()
|
||||
if (names[result]) {
|
||||
result += names[result]++
|
||||
}
|
||||
names[result] = 1
|
||||
return result
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
{
|
||||
"name": "name-server",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "David Mark Clements",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"starwars-names": "^1.6.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"climem": "^1.0.2"
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
const http = require('http')
|
||||
const starwarsName = require('starwars-names').random
|
||||
const names = {}
|
||||
|
||||
http.createServer((req, res) => {
|
||||
res.end(`Your unique name is: ${createName(req)} \n`)
|
||||
}).listen(8080)
|
||||
|
||||
function createName () {
|
||||
var result = starwarsName()
|
||||
if (names[result]) {
|
||||
result += names[result]++
|
||||
}
|
||||
names[result] = 1
|
||||
return result
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
const http = require('http')
|
||||
const starwarsName = require('starwars-names').random
|
||||
const names = {}
|
||||
|
||||
http.createServer((req, res) => {
|
||||
res.end(`Your unique name is: ${createName(req)} \n`)
|
||||
}).listen(8080)
|
||||
|
||||
function createName () {
|
||||
var result = starwarsName()
|
||||
names[result] = names[result] ?
|
||||
names[result] + 1 :
|
||||
1
|
||||
return result + names[result]
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
{
|
||||
"name": "name-server",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "David Mark Clements",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"sillyname": "^0.1.0"
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
{
|
||||
"name": "name-server",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "David Mark Clements",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"starwars-names": "^1.6.0"
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const benchmark = require('benchmark')
|
||||
const slow = require('./slow')
|
||||
const noCollection = require('./no-collections')
|
||||
|
||||
const suite = new benchmark.Suite()
|
||||
|
||||
const numbers = []
|
||||
|
||||
for (let i = 0; i < 1000; i++) {
|
||||
numbers.push(Math.random() * i)
|
||||
}
|
||||
|
||||
suite.add('slow', function () {
|
||||
slow(12, numbers)
|
||||
})
|
||||
|
||||
suite.add('no-collections', function () {
|
||||
noCollection(12, numbers)
|
||||
})
|
||||
|
||||
suite.on('complete', print)
|
||||
|
||||
suite.run()
|
||||
|
||||
function print () {
|
||||
for (var i = 0; i < this.length; i++) {
|
||||
console.log(this[i].toString())
|
||||
}
|
||||
|
||||
console.log('Fastest is', this.filter('fastest').map('name')[0])
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const benchmark = require('benchmark')
|
||||
const slow = require('./slow')
|
||||
const noCollection = require('./no-collections')
|
||||
const noTryCatch = require('./no-try-catch')
|
||||
const funcStatus = require('./func-status')
|
||||
|
||||
const suite = new benchmark.Suite()
|
||||
|
||||
const numbers = []
|
||||
|
||||
for (let i = 0; i < 1000; i++) {
|
||||
numbers.push(Math.random() * i)
|
||||
}
|
||||
|
||||
suite.add('slow', function () {
|
||||
slow(12, numbers)
|
||||
})
|
||||
|
||||
suite.add('no-collections', function () {
|
||||
noCollection(12, numbers)
|
||||
})
|
||||
|
||||
suite.add('no-try-catch', function () {
|
||||
noTryCatch(12, numbers)
|
||||
})
|
||||
|
||||
suite.on('complete', print)
|
||||
|
||||
suite.run()
|
||||
|
||||
function print () {
|
||||
for (var i = 0; i < this.length; i++) {
|
||||
console.log(this[i].toString())
|
||||
}
|
||||
funcStatus('slow', slow)
|
||||
funcStatus('noCollection', noCollection)
|
||||
funcStatus('noTryCatch', noTryCatch)
|
||||
|
||||
console.log('Fastest is', this.filter('fastest').map('name')[0])
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
function printStatus (name, fn) {
|
||||
switch(%GetOptimizationStatus(fn)) {
|
||||
case 1: console.log(`${name} function is optimized`); break;
|
||||
case 2: console.log(`${name} function is not optimized`); break;
|
||||
case 3: console.log(`${name} function is always optimized`); break;
|
||||
case 4: console.log(`${name} function is never optimized`); break;
|
||||
case 6: console.log(`${name} function is maybe deoptimized`); break;
|
||||
case 7: console.log(`${name} function is optimized by TurboFan`); break;
|
||||
default: console.log(`${name} function optimization status unknown`); break;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = printStatus
|
||||
@@ -1,28 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const benchmark = require('benchmark')
|
||||
const noCollection = require('./no-collections')
|
||||
|
||||
const suite = new benchmark.Suite()
|
||||
|
||||
const numbers = []
|
||||
|
||||
for (let i = 0; i < 1000; i++) {
|
||||
numbers.push(Math.random() * i)
|
||||
}
|
||||
|
||||
suite.add('no-collections', function () {
|
||||
noCollection(12, numbers)
|
||||
})
|
||||
|
||||
suite.on('complete', print)
|
||||
|
||||
suite.run()
|
||||
|
||||
function print () {
|
||||
for (var i = 0; i < this.length; i++) {
|
||||
console.log(this[i].toString())
|
||||
}
|
||||
|
||||
console.log('Fastest is', this.filter('fastest').map('name')[0])
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
function divideByAndSum (num, array) {
|
||||
var result = 0
|
||||
try {
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
result += array[i] / num
|
||||
}
|
||||
} catch (err) {
|
||||
// to guard for division by zero
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = divideByAndSum
|
||||
@@ -1,28 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const benchmark = require('benchmark')
|
||||
const noTryCatch = require('./no-try-catch')
|
||||
|
||||
const suite = new benchmark.Suite()
|
||||
|
||||
const numbers = []
|
||||
|
||||
for (let i = 0; i < 1000; i++) {
|
||||
numbers.push(Math.random() * i)
|
||||
}
|
||||
|
||||
suite.add('no-try-catch', function () {
|
||||
noTryCatch(12, numbers)
|
||||
})
|
||||
|
||||
suite.on('complete', print)
|
||||
|
||||
suite.run()
|
||||
|
||||
function print () {
|
||||
for (var i = 0; i < this.length; i++) {
|
||||
console.log(this[i].toString())
|
||||
}
|
||||
|
||||
console.log('Fastest is', this.filter('fastest').map('name')[0])
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
function divideByAndSum (num, array) {
|
||||
var result = 0
|
||||
|
||||
if (num === 0) {
|
||||
return 0
|
||||
}
|
||||
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
result += array[i] / num
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
module.exports = divideByAndSum
|
||||
@@ -1,16 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
function divideByAndSum (num, array) {
|
||||
try {
|
||||
array.map(function (item) {
|
||||
return item / num
|
||||
}).reduce(function (acc, item) {
|
||||
return acc + item
|
||||
}, 0)
|
||||
} catch (err) {
|
||||
// to guard for division by zero
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = divideByAndSum
|
||||
@@ -1,38 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const benchmark = require('benchmark')
|
||||
const slow = require('./slow')
|
||||
const noCollection = require('./no-collections')
|
||||
const noTryCatch = require('./no-try-catch')
|
||||
|
||||
const suite = new benchmark.Suite()
|
||||
|
||||
const numbers = []
|
||||
|
||||
for (let i = 0; i < 1000; i++) {
|
||||
numbers.push(Math.random() * i)
|
||||
}
|
||||
|
||||
suite.add('slow', function () {
|
||||
slow(12, numbers)
|
||||
})
|
||||
|
||||
suite.add('no-collections', function () {
|
||||
noCollection(12, numbers)
|
||||
})
|
||||
|
||||
suite.add('no-try-catch', function () {
|
||||
noTryCatch(12, numbers)
|
||||
})
|
||||
|
||||
suite.on('complete', print)
|
||||
|
||||
suite.run()
|
||||
|
||||
function print () {
|
||||
for (var i = 0; i < this.length; i++) {
|
||||
console.log(this[i].toString())
|
||||
}
|
||||
|
||||
console.log('Fastest is', this.filter('fastest').map('name')[0])
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const benchmark = require('benchmark')
|
||||
const noCollection = require('./no-collections')
|
||||
const noTryCatch = require('./no-try-catch')
|
||||
|
||||
const suite = new benchmark.Suite()
|
||||
|
||||
const numbers = []
|
||||
|
||||
for (let i = 0; i < 1000; i++) {
|
||||
numbers.push(Math.random() * i)
|
||||
}
|
||||
|
||||
suite.add('no-collections', function () {
|
||||
noCollection(12, numbers)
|
||||
})
|
||||
|
||||
suite.on('complete', print)
|
||||
|
||||
suite.run()
|
||||
|
||||
function print () {
|
||||
for (var i = 0; i < this.length; i++) {
|
||||
console.log(this[i].toString())
|
||||
}
|
||||
|
||||
console.log('Fastest is', this.filter('fastest').map('name')[0])
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const benchmark = require('benchmark')
|
||||
const noCollection = require('./no-collections')
|
||||
|
||||
const suite = new benchmark.Suite()
|
||||
|
||||
const numbers = []
|
||||
|
||||
for (let i = 0; i < 1000; i++) {
|
||||
numbers.push(Math.random() * i)
|
||||
}
|
||||
|
||||
suite.add('no-collections', function () {
|
||||
noCollection(12, numbers)
|
||||
})
|
||||
|
||||
suite.on('complete', print)
|
||||
|
||||
suite.run()
|
||||
|
||||
function print () {
|
||||
for (var i = 0; i < this.length; i++) {
|
||||
console.log(this[i].toString())
|
||||
}
|
||||
|
||||
console.log('Fastest is', this.filter('fastest').map('name')[0])
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
function divideByAndSum (num, array) {
|
||||
var result = 0
|
||||
try {
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
result += array[i] / num
|
||||
}
|
||||
} catch (err) {
|
||||
// to guard for division by zero
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = divideByAndSum
|
||||
@@ -1,28 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const benchmark = require('benchmark')
|
||||
const noTryCatch = require('./no-try-catch')
|
||||
|
||||
const suite = new benchmark.Suite()
|
||||
|
||||
const numbers = []
|
||||
|
||||
for (let i = 0; i < 1000; i++) {
|
||||
numbers.push(Math.random() * i)
|
||||
}
|
||||
|
||||
suite.add('no-try-catch', function () {
|
||||
noTryCatch(12, numbers)
|
||||
})
|
||||
|
||||
suite.on('complete', print)
|
||||
|
||||
suite.run()
|
||||
|
||||
function print () {
|
||||
for (var i = 0; i < this.length; i++) {
|
||||
console.log(this[i].toString())
|
||||
}
|
||||
|
||||
console.log('Fastest is', this.filter('fastest').map('name')[0])
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
function divideByAndSum (num, array) {
|
||||
var result = 0
|
||||
|
||||
if (num === 0) {
|
||||
return 0
|
||||
}
|
||||
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
result += array[i] / num
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
module.exports = divideByAndSum
|
||||
@@ -1,16 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
function divideByAndSum (num, array) {
|
||||
try {
|
||||
array.map(function (item) {
|
||||
return item / num
|
||||
}).reduce(function (acc, item) {
|
||||
return acc + item
|
||||
}, 0)
|
||||
} catch (err) {
|
||||
// to guard for division by zero
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = divideByAndSum
|
||||
@@ -1,28 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const benchmark = require('benchmark')
|
||||
const slow = require('./slow')
|
||||
|
||||
const suite = new benchmark.Suite()
|
||||
|
||||
const numbers = []
|
||||
|
||||
for (let i = 0; i < 1000; i++) {
|
||||
numbers.push(Math.random() * i)
|
||||
}
|
||||
|
||||
suite.add('slow', function () {
|
||||
slow(12, numbers)
|
||||
})
|
||||
|
||||
suite.on('complete', print)
|
||||
|
||||
suite.run()
|
||||
|
||||
function print () {
|
||||
for (var i = 0; i < this.length; i++) {
|
||||
console.log(this[i].toString())
|
||||
}
|
||||
|
||||
console.log('Fastest is', this.filter('fastest').map('name')[0])
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
function divideByAndSum (num, array) {
|
||||
var result = 0
|
||||
try {
|
||||
for (var i = 0; i < array.length; i++) {
|
||||
result += array[i] / num
|
||||
}
|
||||
} catch (err) {
|
||||
// to guard for division by zero
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = divideByAndSum
|
||||
@@ -1,14 +0,0 @@
|
||||
{
|
||||
"name": "sync-opt",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"benchmark": "^2.1.0"
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
function divideByAndSum (num, array) {
|
||||
try {
|
||||
array.map(function (item) {
|
||||
return item / num
|
||||
}).reduce(function (acc, item) {
|
||||
return acc + item
|
||||
}, 0)
|
||||
} catch (err) {
|
||||
// to guard for division by zero
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = divideByAndSum
|
||||
Reference in New Issue
Block a user