typeIsExact.js 954 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. // benchmark ways to check if is exact type
  3. // setup
  4. var Thing = function Thing() {},
  5. thing = new Thing(),
  6. objToStr = Object.prototype.toString,
  7. getTypeStr = function getTypeStr2(obj) {
  8. return objToStr.call(obj);
  9. };
  10. // tests
  11. exports.typeIsExact = {
  12. "getTypeStr (Object#toString.call)": function() {
  13. getTypeStr(thing) === "[object Function]";
  14. },
  15. "instanceof, constructor matches": function() {
  16. thing instanceof Thing && thing.constructor === Thing;
  17. },
  18. "truthy, constructor matches": function() {
  19. thing && thing.constructor === Thing;
  20. },
  21. "not undefined, not null, constructor matches": function() {
  22. thing !== undefined && thing !== null && thing.constructor === Thing;
  23. },
  24. "typeof, not null, constructor matches": function() {
  25. typeof thing === "object" && thing !== null && thing.constructor === Thing;
  26. },
  27. };
  28. // if run directly run benchmarks
  29. if (!module.parent) require("benchmarksman").runner(exports);