Code files

This commit is contained in:
Akhil
2017-07-31 11:33:31 +05:30
parent 073a63ebdd
commit b9ae409ec0
801 changed files with 17535 additions and 0 deletions
+23
View File
@@ -0,0 +1,23 @@
'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()
})
})
})
+26
View File
@@ -0,0 +1,26 @@
'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()
})
+17
View File
@@ -0,0 +1,17 @@
{
"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"
}
}
+19
View File
@@ -0,0 +1,19 @@
'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)
})
+56
View File
@@ -0,0 +1,56 @@
'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)
})
+29
View File
@@ -0,0 +1,29 @@
'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)
})
+33
View File
@@ -0,0 +1,33 @@
'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)
})
+26
View File
@@ -0,0 +1,26 @@
'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)
})
+27
View File
@@ -0,0 +1,27 @@
'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)
})
@@ -0,0 +1,15 @@
{
"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"
}
}
@@ -0,0 +1,14 @@
'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)
+14
View File
@@ -0,0 +1,14 @@
{
"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"
}
}
@@ -0,0 +1,15 @@
{
"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"
}
}
@@ -0,0 +1,14 @@
'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)
@@ -0,0 +1,7 @@
doctype html
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body
h1= title
+10
View File
@@ -0,0 +1,10 @@
'use strict'
const express = require('express')
const app = express()
app.get('/hello', (req, res) => {
res.send('hello world')
})
app.listen(3000)
+16
View File
@@ -0,0 +1,16 @@
{
"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"
}
}
+12
View File
@@ -0,0 +1,12 @@
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)
+7
View File
@@ -0,0 +1,7 @@
doctype html
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body
h1= title
@@ -0,0 +1,16 @@
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
}
@@ -0,0 +1,18 @@
{
"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"
}
}
+16
View File
@@ -0,0 +1,16 @@
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
}
+15
View File
@@ -0,0 +1,15 @@
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]
}
@@ -0,0 +1,15 @@
{
"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"
}
}
+15
View File
@@ -0,0 +1,15 @@
{
"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"
}
}
View File
+33
View File
@@ -0,0 +1,33 @@
'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])
}
@@ -0,0 +1,42 @@
'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])
}
@@ -0,0 +1,15 @@
'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
@@ -0,0 +1,28 @@
'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])
}
@@ -0,0 +1,15 @@
'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
@@ -0,0 +1,28 @@
'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])
}
@@ -0,0 +1,17 @@
'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
@@ -0,0 +1,16 @@
'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
@@ -0,0 +1,38 @@
'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])
}
@@ -0,0 +1,29 @@
'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])
}
@@ -0,0 +1,28 @@
'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])
}
@@ -0,0 +1,15 @@
'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
@@ -0,0 +1,28 @@
'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])
}
@@ -0,0 +1,17 @@
'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
@@ -0,0 +1,16 @@
'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
+28
View File
@@ -0,0 +1,28 @@
'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])
}
+15
View File
@@ -0,0 +1,15 @@
'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
+14
View File
@@ -0,0 +1,14 @@
{
"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"
}
}
+16
View File
@@ -0,0 +1,16 @@
'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