-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflyweight.js
106 lines (87 loc) · 2.34 KB
/
flyweight.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'use strict'
/**
* Flyweight Pattern
* - Conserve memory by sharing portions of an object
* between objects
* - Share data across objects
* - ONLY USEFUL WHEN WE HAVE LARGE NUMBER OF OBJECTS
*/
let projects = ['none', 'p1', 'p2', 'p3']
let priorities = [1, 2, 3, 4, 5]
let users = ['Felipe', 'James', 'Kate', 'Nathan']
let completed = [true, false]
let Task1 = function (data) {
this.name = data.name
this.priority = data.priority
this.project = data.project
this.user = data.user
this.completed = data.completed
}
let Task2 = function (data) {
this.name = data.name // The only unique!!!
this.flyweight = FlyweightFactory.get(data.project, data.priority, data.user, data.completed)
}
// FLYWEIGHT
function Flyweight (project, priority, user, completed) {
this.priority = priority
this.project = project
this.user = user
this.completed = completed
}
let FlyweightFactory = (function () {
let flyweights = {}
// If key not exists, we create a new one
let get = function (project, priority, user, completed) {
if (!flyweights[project + priority + user + completed]) {
flyweights[project + priority + user + completed] = new Flyweight(project, priority, user, completed)
}
return flyweights[project + priority + user + completed]
}
return {
get: get
}
}())
function TaskCollection (T) {
let tasks = {}
let count = 0
let add = function (data) {
tasks[data.name] = new T(data)
count++
}
let get = function (name) {
return tasks[name]
}
let getCount = function () {
return count
}
return {
add: add,
get: get,
getCount: getCount
}
}
let tasks1 = new TaskCollection(Task1)
let tasks2 = new TaskCollection(Task2)
function testCreation (col) {
let initialMemory = process.memoryUsage().heapUsed
for (let i = 0; i < 1000000; i++) {
col.add({
name: `task${i}`,
priority: priorities[Math.floor((Math.random() * 5))],
project: projects[Math.floor((Math.random() * 4))],
user: users[Math.floor((Math.random() * 4))],
completed: completed[Math.floor((Math.random() * 2))]
})
}
let afterMemory = process.memoryUsage().heapUsed
let memUsage = ((afterMemory - initialMemory) / 10000000)
return {
memUsage: memUsage,
taskCount: col.getCount()
}
}
module.exports = {
tasks1: tasks1,
tasks2: tasks2,
testCreation: testCreation
}