typeIsExact.js 734 B

123456789101112131415161718192021222324252627282930313233
  1. "use strict";
  2. // benchmark ways to check if is exact type
  3. // setup
  4. function Thing() {}
  5. var thing = new Thing();
  6. // tests
  7. exports.typeIsExact = {
  8. "instanceof, constructor matches": function() {
  9. thing instanceof Thing && thing.constructor === Thing;
  10. },
  11. "not undefined, not null, constructor matches": function() {
  12. thing !== undefined && thing !== null && thing.constructor === Thing;
  13. },
  14. "truthy, constructor matches": function() {
  15. thing && thing.constructor === Thing;
  16. },
  17. "typeof, not null, constructor matches": function() {
  18. typeof thing === "object" && thing !== null && thing.constructor === Thing;
  19. },
  20. };
  21. // if run directly run benchmarks
  22. if (!module.main) return require("benchmarksman").runner(exports);