avoid-Date-timing.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /**
  2. * @fileoverview Disallow using Date for timing in performance sensitive code
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. */
  8. "use strict";
  9. module.exports = {
  10. meta: {
  11. docs: {
  12. url:
  13. "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/avoid-Date-timing.html",
  14. },
  15. type: "problem",
  16. },
  17. create(context) {
  18. return {
  19. CallExpression(node) {
  20. let callee = node.callee;
  21. if (
  22. callee.type !== "MemberExpression" ||
  23. callee.object.type !== "Identifier" ||
  24. callee.object.name !== "Date" ||
  25. callee.property.type !== "Identifier" ||
  26. callee.property.name !== "now"
  27. ) {
  28. return;
  29. }
  30. context.report(
  31. node,
  32. "use performance.now() instead of Date.now() for timing " +
  33. "measurements"
  34. );
  35. },
  36. NewExpression(node) {
  37. let callee = node.callee;
  38. if (
  39. callee.type !== "Identifier" ||
  40. callee.name !== "Date" ||
  41. node.arguments.length
  42. ) {
  43. return;
  44. }
  45. context.report(
  46. node,
  47. "use performance.now() instead of new Date() for timing " +
  48. "measurements"
  49. );
  50. },
  51. };
  52. },
  53. };