Skip to content

Instantly share code, notes, and snippets.

@dmitryrck
Last active March 13, 2019 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitryrck/8e5718d4c71149bb41dacc5751a420ab to your computer and use it in GitHub Desktop.
Save dmitryrck/8e5718d4c71149bb41dacc5751a420ab to your computer and use it in GitHub Desktop.
// npm i microtime benchmark
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;
var data = require("./data.json")
suite.add("map+filter", function() {
const result = data.map(record => {
return record.period.split("-")[0]
}).filter((elem, index, self) => {
return index == self.indexOf(elem)
})
})
.add("do+while", function() {
if (data.length > 0) {
let result = records.map(record => {
return parseInt(record.period.split("-")[0])
})
let current
let arr2 = []
do {
current = result.pop()
if (arr2.indexOf(current) == -1) {
arr2.push(current)
}
} while (result.length > 0)
return arr2
} else {
return []
}
})
.on("cycle", function(event) {
console.log(String(event.target));
})
.on("complete", function() {
console.log("Fastest is " + this.filter("fastest").map("name"));
})
.run({ "async": true });
function getYears(records) {
if (records.length > 0) {
let result = records.map(record => {
return parseInt(record.period.split("-")[0])
})
let current
let arr2 = []
do {
current = result.pop()
if (arr2.indexOf(current) == -1) {
arr2.push(current)
}
} while (result.length > 0)
return arr2
} else {
return []
}
}
const data = [
{ "period": "2019-02-01", value: "10" },
{ "period": "2019-01-01", value: "1" },
{ "period": "2018-01-01", value: "2" },
{ "period": "2017-01-01", value: "3" },
]
console.log(getYears(data))
const data = [
{ "period": "2019-02-01", value: "10" },
{ "period": "2019-01-01", value: "1" },
{ "period": "2018-01-01", value: "2" },
{ "period": "2017-01-01", value: "3" },
]
const result = data.map(record => {
return record.period.split("-")[0]
}).filter((elem, index, self) => {
return index == self.indexOf(elem)
})
console.log(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment