-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy path4-array.js
90 lines (77 loc) · 2.05 KB
/
4-array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'use strict';
const benchmark = require('./2-benchmark.js');
// Define Data Source
const data1 = [
{ name: 'Marcus Aurelius',
birth: new Date('212-04-26'),
city: 'Rome' },
{ name: 'Victor Glushkov',
birth: new Date('1923-08-24'),
city: 'Rostov on Don' },
{ name: 'Ibn Arabi',
birth: new Date('1165-11-16'),
city: 'Murcia' },
{ name: 'Mao Zedong',
birth: new Date('1893-12-26'),
city: 'Shaoshan' },
{ name: 'Rene Descartes',
birth: new Date('1596-03-31'),
city: 'La Haye en Touraine' },
];
const data2 = [
['Marcus Aurelius', '212-04-26', 'Rome'],
['Victor Glushkov', '1923-08-24', 'Rostov on Don'],
['Ibn Arabi', '1165-11-16', 'Murcia'],
['Mao Zedong', '1893-12-26', 'Shaoshan'],
['Rene Descartes', '1596-03-31', 'La Haye en Touraine']
];
const metadata = {
name: 'string',
birth: 'Date',
city: 'string',
age() {
const difference = new Date() - this.birth;
return Math.floor(difference / 31536000000);
}
};
// Prepare prototype
function Person() {}
let index = 0;
for (const name in metadata) {
buildGetter(Person.prototype, name, metadata[name], index++);
}
function buildGetter(proto, fieldName, fieldType, fieldIndex) {
if (fieldType === 'Date') {
Object.defineProperty(proto, fieldName, {
get() {
return new Date(this[fieldIndex]);
}
});
} else if (typeof fieldType === 'function') {
Object.defineProperty(proto, fieldName, { get: fieldType });
} else {
Object.defineProperty(proto, fieldName, {
get() {
return this[fieldIndex];
}
});
}
}
data1.forEach((person) => Object.setPrototypeOf(person, Person.prototype));
data2.forEach((person) => Object.setPrototypeOf(person, Person.prototype));
//data2.forEach(person => person.__proto__ = Person.prototype);
// Define query
const query = (person) => (
person.name !== '' &&
person.age > 18 &&
person.city === 'Rome'
);
// Execute tests
benchmark.do(1000000, [
function filterObjects() {
data1.filter(query);
},
function filterArrays() {
data2.filter(query);
}
]);