-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.ts
128 lines (106 loc) · 3.97 KB
/
script.ts
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
interface Payroll {
empNo: string;
vacationDays: number;
}
interface AddressBook {
empNo: string;
email: string;
}
interface WorkHistory {
empNo: string;
name: string;
yearsEmployed: number;
}
interface EmailApi {
sendEmail(email: string, body: string);
}
function NewEmailVacationGrant(
emailApi: EmailApi,
histories: WorkHistory[],
addresses: AddressBook[],
payroll: Payroll[]
) {
// Assumption:
// - histories.length == addresses.length == payroll.length
// create hash maps for addresses and payroll; this removes the need to
// use the slow, linear Array.find() method
var mappedAddresses:AddressBook[] = [];
for (var i:number = 0; i < addresses.length; i++) {
mappedAddresses[addresses[i].empNo] = Object.assign({}, addresses[i]);
}
var mappedPayroll:Payroll[] = [];
for (var i:number = 0; i < payroll.length; i++) {
mappedPayroll[payroll[i].empNo] = Object.assign({}, payroll[i]);
}
for (var i:number = 0; i < histories.length; i++) {
var employee:WorkHistory = histories[i];
var address:AddressBook = mappedAddresses[employee.empNo];
var newVacationBalance:number = employee.yearsEmployed + mappedPayroll[employee.empNo].vacationDays;
emailApi.sendEmail(
address.email,
`Dear ${employee.name},\n` +
`Based on your ${employee.yearsEmployed} years of employment, you have been granted ${employee.yearsEmployed} bonus days of vacation, bringing your total to ${newVacationBalance}.`
);
}
}
function OldEmailVacationGrant(
emailApi: EmailApi,
histories: WorkHistory[],
addresses: AddressBook[],
payroll: Payroll[]
) {
for (var i:number = 0; i < histories.length; i++) {
let employee = histories[i];
let address = addresses.find(x => x.empNo==employee.empNo);
let employeePayroll = payroll.find(x => x.empNo==employee.empNo);
let newVacationBalance = employee.yearsEmployed + employeePayroll.vacationDays;
emailApi.sendEmail(
address.email,
`Dear ${employee.name}\n` +
`based on your ${employee.yearsEmployed} years of employment, you have been granted ${employee.yearsEmployed} days of vacation, bringing your total to ${newVacationBalance}`
);
}
}
(function CompareOldVSNew() {
// generate dummy data for testing
var payroll:Payroll[] = [];
var addressBook:AddressBook[] = [];
var workHistory:WorkHistory[] = [];
for (var i:number = 0; i < 10000; i++) {
var empNo:string = Math.floor((Math.random() * 10000000)).toString();
payroll.push({
empNo: empNo,
vacationDays: 10 // default starting point
});
addressBook.push({
empNo: empNo,
email: 'foo@' + empNo + '.com'
});
workHistory.push({
empNo: empNo,
name: 'Bar ' + empNo,
yearsEmployed: Math.floor(Math.random() * 15) + 1 // 1-15 years
});
}
// dummy EmailClient
class EmailClient {
sendEmail(email: string, body: string):void {
// console.log("Sending email to: " + email + ".\n");
// console.log(body + "\n");
}
}
var emailClient:EmailApi = new EmailClient();
var newAverage:number = 0;
var oldAverage:number = 0;
var sampleRuns:number = 10;
for (var i:number = 0; i < sampleRuns; i++) {
const newStart:number = Date.now();
NewEmailVacationGrant(emailClient, workHistory, addressBook, payroll);
newAverage += Date.now() - newStart;
const oldStart:number = Date.now();
OldEmailVacationGrant(emailClient, workHistory, addressBook, payroll);
oldAverage += Date.now() - oldStart;
}
console.log('Average runtime of new implementation: ' + (newAverage / sampleRuns).toString() + 'ms.');
console.log('Average runtime of old implementation: ' + (oldAverage / sampleRuns).toString() + 'ms.');
})();