Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
How to set a time limit for a program to execute?

I am currently writing tests for a sort of Diagnostic test for a friend. I did a Python version of it, and what I want to do is to stop the test if the problem is taking too long. For example in Python this is how I did it:

@timeout_decorator.timeout(5)
def test_intersection_that_will_break_if_solution_is_too_slow(self):
    a = [i for i in range(0, 50000)]
    b = [i for i in range(0, 50000)]
    result = intersection.intersection(a, b)
    self.assertEqual(result, a)

In JavaScript, in Chaijs, I have this, but don't know how to stop the execution after a certain period of time. I am not the best at js either or chai, but maybe some sort of setTimeOut?

context('if a = [0,1,2,3...,49999] and b = [0,1,2,3...,49999] ', () => {
    it("should return [0,1,2,3...,49999] ", () => {
        const a = [];
        const b = [];
        for (let i = 0; i < 50000; i += 1) {
            a.push(i);
            b.push(i);
        }
        result = intersection(a, b);
        expect(result).to.eql(a);
    })
})

I want to know how to do that in Chai, because when I do the tests for a fibonacci problem, I want to stop the execution if it is taking too long(although I believe that chai/mocha stops the execution after 2000 ms but I still want to encourage my friend to look for a more optimal solution rather than getting the right answer with a red (1193ms)).

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!