-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathReactMicroBenchmark.js
More file actions
130 lines (122 loc) · 4.27 KB
/
ReactMicroBenchmark.js
File metadata and controls
130 lines (122 loc) · 4.27 KB
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
129
130
var React = require('./js/react');
var CommentForm = React.createClass({displayName: "CommentForm",
handleSubmit: function (e) {
e.preventDefault();
var author = this.refs.author.getDOMNode().value.trim();
var text = this.refs.text.getDOMNode().value.trim();
if (!author || !text) {
return;
}
this.props.onCommentSubmit({author: author, text: text});
this.refs.author.getDOMNode().value = '';
this.refs.text.getDOMNode().value = '';
},
render: function () {
return (
React.createElement("form", {className: "commentForm", onSubmit: this.handleSubmit},
React.createElement("input", {type: "text", placeholder: "Your name", ref: "author"}),
React.createElement("input", {type: "text", placeholder: "Say something...", ref: "text"}),
React.createElement("input", {type: "submit", value: "Post"})
)
);
}
});
var Comment = React.createClass({displayName: "Comment",
render: function () {
var rawMarkup = this.props.children.toString();
return (
React.createElement("div", {className: "comment"},
React.createElement("h2", null, this.props.author),
React.createElement("span", {dangerouslySetInnerHTML: {__html: rawMarkup}})
)
);
}
});
var CommentList = React.createClass({displayName: "CommentList",
render: function () {
var commentNodes = this.props.data.map(function (comment, index) {
return (
React.createElement(Comment, {author: comment.author, key: index},
comment.text
)
);
});
return (
React.createElement("div", {className: "commentList"},
commentNodes
)
);
}
});
var CommentBox = React.createClass({displayName: "CommentBox",
handleCommentSubmit: function (comment) {
var comments = this.state.data;
comments.push(comment);
this.setState({data: comments}, function () {
$.ajax({
url: this.props.url,
dataType: 'json',
type: 'POST',
data: comment,
success: function (data) {
this.setState({data: data});
}.bind(this),
error: function (xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
});
},
loadCommentsFromServer: function () {
$.ajax({
url: this.props.url,
dataType: 'json',
success: function (data) {
this.setState({data: data});
}.bind(this),
error: function (xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
getInitialState: function () {
return {data: this.props.data};
},
componentDidMount: function () {
this.loadCommentsFromServer();
setInterval(this.loadCommentsFromServer, this.props.pollInterval);
},
render: function () {
return (
React.createElement("div", {className: "commentBox"},
React.createElement("h1", null, "Comments"),
React.createElement(CommentList, {data: this.state.data}),
React.createElement(CommentForm, {onCommentSubmit: this.handleCommentSubmit})
)
);
}
});
var renderServer = function (comments) {
return React.renderToString(
React.createElement(CommentBox, {data: comments, url: "comments.json", pollInterval: 5000})
);
};
module.exports = {
renderServer: renderServer
};
var log = console.log.bind(console);
var timestamp = function() { return process.hrtime()[1] / 1e6; };
if (require.main === module) {
var comments = [];
var NUM_COMMENTS = 10;
for (var i = 0; i < NUM_COMMENTS; ++i) {
comments.push({author:"Name "+i, text:"This is comment #"+i+"."});
}
for (i = 0; i < 10000; ++i) {
var start, stop;
start = timestamp();
renderServer(comments);
stop = timestamp();
log('Run #' + (i + 1) + ':', (stop - start), 'ms');
}
}