knownSetHas.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. // benchmark ways to test if var is in a known set
  3. // setup
  4. var name = "things",
  5. arr = ["foo", "bar", "baz", "qux", "stuff", "things"],
  6. obj = {foo:"foo", bar:"bar", baz:"baz", qux:"qux", stuff:"stuff", things:"things"};
  7. // tests
  8. exports.knownSetHas = {
  9. "array indexOf closure": function() {
  10. return arr.indexOf(name) !== -1;
  11. },
  12. "array indexOf inline": function() {
  13. return ["foo", "bar", "baz", "qux", "stuff", "things"].indexOf(name) !== -1;
  14. },
  15. "object in closure": function() {
  16. return name in obj;
  17. },
  18. "object in inline": function() {
  19. return name in {foo:"foo", bar:"bar", baz:"baz", qux:"qux", stuff:"stuff", things:"things"};
  20. },
  21. "switch": function() {
  22. switch (name) {
  23. case "foo":
  24. case "bar":
  25. case "baz":
  26. case "qux":
  27. case "stuff":
  28. case "things":
  29. return true;
  30. default:
  31. return false;
  32. }
  33. },
  34. "if": function() {
  35. return name === "foo" ||
  36. name === "bar" ||
  37. name === "baz" ||
  38. name === "qux" ||
  39. name === "stuff" ||
  40. name === "things";
  41. },
  42. };
  43. // if run directly run benchmarks
  44. if (!module.main) return require("benchmarksman").runner(exports);