|
const { Map, List } = require('immutable'); |
|
const {Benchmark} = require('benchmark'); |
|
var suite = new Benchmark.Suite; |
|
|
|
function objectsManipulation (suite) { |
|
return suite |
|
.add('object - vanilla immutable', function() { |
|
let person = { name: 'Aschen', age: 28 }; |
|
|
|
// add property |
|
person = { ...person, city: 'ahangama' }; |
|
|
|
// update property |
|
person = { ...person, age: 42 }; |
|
}) |
|
.add('object - immutable.js', function() { |
|
let person = new Map({ name: 'aschen', age: 28 }); |
|
|
|
// add property |
|
person = person.set('city', 'ahangama'); |
|
|
|
// set property |
|
person = person.set('age', 42); |
|
}) |
|
.add('object - mutable', function() { |
|
let person = { name: 'Aschen', age: 28 }; |
|
|
|
// add property |
|
person.city = 'ahangama'; |
|
|
|
// update property |
|
person.age = 42; |
|
}); |
|
} |
|
|
|
function arraysManipulation (suite) { |
|
return suite |
|
.add('array - vanilla immutable', function() { |
|
let cities = ['tirana', 'montpellier', 'minsk', 'tbilisi', 'ahangama']; |
|
|
|
// push city |
|
cities = [...cities, 'adrasan']; |
|
|
|
// remove city "minsk" |
|
cities = [...cities.slice(0, 2), ...cities.slice(3)]; |
|
}) |
|
.add('array - immutable.js', function() { |
|
let cities = new List(['tirana', 'montpellier', 'minsk', 'tbilisi', 'ahangama']); |
|
|
|
// push city |
|
cities = cities.push('adrasan'); |
|
|
|
// remove city "minsk" |
|
cities = cities.delete(2); |
|
}) |
|
.add('array - mutable', function() { |
|
let cities = ['tirana', 'montpellier', 'minsk', 'tbilisi', 'ahangama']; |
|
|
|
// push city |
|
cities.push('adrasan'); |
|
|
|
// remove city "minsk" |
|
cities.splice(2); |
|
}); |
|
} |
|
|
|
// add tests |
|
objectsManipulation(suite) |
|
arraysManipulation(suite) |
|
// add listeners |
|
.on('cycle', function(event) { |
|
console.log(String(event.target)); |
|
}) |
|
.on('complete', function() { |
|
console.log('Fastest is ' + this.filter('fastest').map('name')); |
|
}) |
|
// run async |
|
.run(); |