javascript String vs toString()(http://jsbench.github.io/#df630cae631d261fd4298d5dd0b6f2e2)#jsbench

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript String vs toString()(http://jsbench.github.io/#df630cae631d261fd4298d5dd0b6f2e2)#jsbench 相关的知识,希望对你有一定的参考价值。

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8"/>
	<title>String vs toString()</title>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
	<script src="./suite.js"></script>
</head>
<body>
	<h1>Open the console to view the results</h1>
	<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2>
</body>
</html>
"use strict";

(function (factory) {
	if (typeof Benchmark !== "undefined") {
		factory(Benchmark);
	} else {
		factory(require("benchmark"));
	}
})(function (Benchmark) {
	var suite = new Benchmark.Suite;

	Benchmark.prototype.setup = function () {
		const data = {id: 38}
	};


	suite.add("data.id.toString()", function () {
		data.id.toString()
	});

	suite.add("String(data.id)", function () {
		String(data.id)
	});

	suite.on("cycle", function (evt) {
		console.log(" - " + evt.target);
	});

	suite.on("complete", function (evt) {
		console.log(new Array(30).join("-"));

		var results = evt.currentTarget.sort(function (a, b) {
			return b.hz - a.hz;
		});

		results.forEach(function (item) {
			console.log((idx + 1) + ". " + item);
		});
	});

	console.log("String vs toString()");
	console.log(new Array(30).join("-"));
	suite.run();
});

以上是关于javascript String vs toString()(http://jsbench.github.io/#df630cae631d261fd4298d5dd0b6f2e2)#jsbench 的主要内容,如果未能解决你的问题,请参考以下文章

javascript的隐式类型转换

07.重写ToSting()方法

javascript String vs toString()(http://jsbench.github.io/#df630cae631d261fd4298d5dd0b6f2e2)#jsbench

android开发Tost工具类管理

Javascript Garden

前端获取Long类型精度丢失解决办法